Desktop project sharing an EF Code First Model with a MVC4 Web App Project Solution - entity-framework

I recently added a Visual Studio 2012 Desktop Project to a Visual Studio 2012 Solution that contains several Web Apps Projects and 1 Class LIbrary Project. The Class Library handles the EF 5 Code First Database Model that is shared for all the Web App in the solution.
The class LIbrary Project is the only one I used to update the database. The rest of the web projects are just consumers of that DLL library.
Now I'm trying to add a Visual Studio 2012 Desktop Application project to the same solution. The goal is for the Desktop App to use the same Class LIbrary DLL to interact with the database.
I added the Desktop Project to the solution and created the reference to the class library DLL. After I made changes on it to access the database through this DLL, like:
ContextDB db = new ContextDB();
ClassLib.Models.Gallery gallery = (from g in db.Galleries where g.GalleryId == galleryId select g).SingleOrDefault();
After the desktop Project is built I ran it and the first error that shows is:
The model backing the 'ContextDB' context has changed since the database was created. Consider using Code First Migrations to update the database
Then I tried to solve using this line of code just before the ContextDB definition:
Database.SetInitializer<ContextDB>(null);
ContextDB db = new ContextDB();
That solved that problem but now it is showing another error related with INVALID COLUMNS in almost all the columns on the table, the error is:
Invalid column name 'EventId'.
Invalid column name 'WebsiteId'.
Invalid column name 'Slug'.....
and so on for all the columns on the table..
There's nothing wrong with the database and other projects in the solution are using the Class Library without any problem.
I'm not sure what to do now.

Related

I am not able to add migrations in visual studio 2022 , dot net core based project I am seeing the below error and I have tried different methods

I am running a migration script using package manager console after setting dependencies in corresponding files in dot net core based project and i am receiving below error after running the below migration command in package manager console ,
Add-Migration "InitialCreate"
Error received :
Unable to create an object of type 'PaymentDetailContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
I have gone through different ways to troubleshoot this issue inorder to create a migration file
I am logging using windows authentication method and kindly help me to resolve this issue in visual studio 2022
Entity framework Dot Net Core Error In Adding Migrations
I have tried to check all the corresponding files whether any spelling mistakes in connection string file , added corresponding packages using browse option and installed
I am expecting this error to be resolved (below one ) after executing the script
Add-Migration "InitialCreate"
Unable to create an object of type 'PaymentDetailContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
and thereby generating the migration script inside web api project

Code First Migration and creating a mdf in a class library

I have a Solution with three projects. One are the models, another is the repository and context configuration and the last one is a Web application.
The things is that when i configure the Entity Framework in the Repository Project and add the migrations, and i try to execute update-migration nuget console this throw me and error.
I need the database in a mdf file so the app.config look this way:
The error when i try to update-migration is:
The physical file name "/ExampleDB.mdf" may be incorrect
By the error i understand that |DataDirectory| is not working and i can't set it with
AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory()); because this project is a class library.
Thanks a lot

UnintentionalCodeFirstException thrown by mstest

I have a test project which references another project, which in turn references a third project containing my Entity Framework 5.0 file (edmx). I find that I can run database-driven unit tests directly from Visual Studio, but when I run from mstest I get the following errors:
System.Data.Entity.Infrastructure.UnintentionalCodeFirstException:
Code generated using the T4 templates for Database First and Model
First development may not work correctly if used in Code First mode.
To continue using Database First or Model First ensure that the Entity
Framework connection string is specified in the config file of
executing application.
I have non-database connecting unit tests which run fine. I've fiddled with my various app.config EF connection strings and they seem to be OK (every tests runs fine on Visual Studio). The problem is when I run mstest from a command line on my desktop or on a build server. This is the command I use:
MSTest.exe /nologo /searchpathroot:"\Binaries"
/resultsfileroot:"\TestResults"
/testcontainer:"\Binaries\driver.orderedtest"
/testcontainer:"\Binaries\EF.DataAccess.dll"
/publish:"http://"
/publishbuild:"vstfs:///Build/Build/99" /teamproject:"ProjectName"
/platform:"Any CPU" /flavor:"Debug" /noisolation /detail:errormessage
/detail:errorstacktrace /detail:stderr
I have references to all dll's in the test project, as outlined here: Entity Framework custom tool does not correctly embed or load the model as resource files
I've been on this for a few days off and on, and it looks to me that this is an EF 5.0 bug, but according to the connect link this should be fixed, but it doesn't seem to be the case.

Using Soap in Shared Mono Library for WP 7 and Android

I'm currently working on an shared library based on mono, where I want to put as much business logic of my app as possible.
I used this helpful tutorial.
I managed putting the whole logic for rest-requests in this shared library, but now I'm stuck with soap.
I used the wsdl command of mono to generate Client Stubcode from my wsdl (as described here http://www.mono-project.com/Web_Services).
When I put the generated class to my C# library, which is the root project of my shared library, there is a warning that the Reference to System.Web.Services cannot be found.
So I included the System.Web.Services.dll manually.
For the Android Library Project I added a Reference to ...\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Web.Services.dll. It compiles without warnings.
But now it comes to the Windows Phone Library Project.
There is no System.Web.Services.dll for WP 7.5, right? I tried with the Mono-Touch dll but it gives me a lot compilation Errors.
Someone knows how I can get out of this?
I actually had some issues with the generated WSDL myself. Turns out that the classes that were generated through the "Create Web Reference" piece of Visual Studio inside of a Mono for Android project ended up causing some big issues when connected to a WCF Web Service. Not sure where I ran into this information, but this is what I ended up doing.
What you need to do is manually create a Service Reference using the SILVERLIGHT SVCUtil.
On my development system it was located here:
C:\Program Files (x86)\Microsoft SDKs\Silverlight\v3.0\Tools
I called it with the below command line:
slsvcutil.exe http://localhost/<path to WCF service endpoint>/service.svc /directory:"<temp directory to store generated cs file>" /noConfig /namespace:"*,<Full namespace of the generated class>"
That will actually generate a CS file that is saved into the path specified by the /directory tag above. Copy that generated cs file to your project directory and then include it in the project.
The problem that I was having that forced me to look for another option was that I was able to pull the data properly using the WSDL generated through the "Add Web Reference" option in Visual Studio, but as soon as I tried to pass the data back up the wire to the web service, everything blew up. Using the Service Reference generated by the Silverlight Service Util actually generated all the code properly for Async operations and after learning how to properly manage those Async operations everything works like a dream.
Since you are generating this new WSDL using the Silverlight Utility, it should work just fine through Windows Phone 7. I believe that the DLL to reference for all of this is the System.ServiceModel dll.
I wish I could remember where I ran across this information, as I would like to give the original author credit, but unfortunately, I don't recall that.
Hope that helps out!
Chaitanya Marvici

Entity Framework 4 CTP 5 Code First Development

I am trying to do some tests using EF4 + CTP5 with code first development by writing POCO classes and have faced the following problems
Connecting to existing database throws me the exception:
Model compatibility cannot be checked because the database does not contain model metadata
While I can create a new database from code by giving a new file name in a connection string, not able to open the file (database) using the Management Studio after running few tests against, the Management studio doesn't allow to add the file when trying to attach the database with no specific error detail provided.
For #1, did you try adding the following to your global.asax?
Database.SetInitializer<CustomContextNameHere>(null);
where "CustomContextNameHere" is your context object.
As far as #2 goes, I am assuming you mean the default ASPNETDB.MDF file. Have you tried opening the file with the Server Explorer in VS? It may open right up if you double-click on it in the solution.