I am currently using EF 4 with my ASP.NET Website to access a MySql database. This works fine. However I now want another Web Site project to access the same entities. How do I set this up?
I can't just reference the original website as it's a Web Site, not a Web Application. So presumably, I need to put the Entity Data Model in its own project and compile to a DLL. But...
Which project type?
Do I just cut and paste the DataModel.edmx and DataModel.Designer.cs, compile and add a reference in both websites? What about namespaces?
Where do I put the connection string? At the moment it's in my project's Web.config.
I really have no idea where to begin - I've just followed tutorials to get EF working up to now! I'd really appreciate step-by-step instructions if anyone has time. Thanks.
The model should be placed in a new class library project. My preference would be to recreate the model at this point based on the existing model. For the namespace I like to use {CompanyName}.DataAccess. Remove the old model from your web site project, add a reference to the new class library project and build the web site project. The website project will break in many places, but it should be a simple matter of changing the namespace to the new data access assembly. I prefer this method as to cut/paste because now you have nice clean namespaces. Be careful of any places you may have strings with entity names in them, like if you were using Include (if you are using EF 4 and lazy loading, this should not be a problem). Leave the connection string in web.config for both of the web site projects. When you create the model in the class library, it will add a connection string in app.config. That is OK, it is just there so the model knows how to connect to the database when you refresh it.
Related
I've create two MVC and Web API .Net Core projects in one solution, in first project i've added model Phone and DataContext for it, made migration and update-database. In second project i've added equals connectionstring,model and DataContext, but when i try make the update-database i get the next error:
There is already an object named 'Phones' in the database.
Tell me please the right way how to configure database, datacontext and models in two projects to use one database ?
If you use two separate DbContexts, each with their own migrations, they'll consider the database to be theirs and reapply the changes that the other project already did, resulting in conflicts such as the one you observed.
Simply move the Entity Framework code into a shared class library and reference that library from both implementing projects, so all state about the database is shared.
I have 2 VS solutions.
The first solution has a WebUI project in .Net 4.5.2 and a Domain project also in .Net 4.5.2. The WebUI depends on the Domain and holds a reference to it. The Domain builds first. Everything works.
The second solution has a WebUI project in .Net 4.6. I "added" the Domain project from the first solution here (by clicking right mouse on my solution name --> Add --> Existing project).
I added a reference here also and the Domain builds before the WebUI just as in my first solution. Things work correct, for example, I can create a viewmodel in my WebUI and reference to a domain class. I can also acces the domain classes after using an import statement in my controllers. See second pic where I use the person class and contact class. This all works fine. I have some issues however.
In the WebUi's when I want to generate a mvc5 controller using entity framework in my first solution I see in the model class dropdown list my domain entities.
However, I don't see these entities(Domain.DbEntities) in my second solution in the dropdown, which I find very strange since I can acces the domain entities from whitin code-files which you can see in de background(i.e. 'person' and 'contact' in my EditPerson(int id) action method
Anybody an idea why?
Maybe the different framework version is the problem.
Have you build the project already?
See also: Add Controller Model Classes not shown
I've an existing MVC3 project that implements a certain functionality, this project has it's own views, and a separate Database.
now I'm required to use the same functionality inside one of my orchard project,so I thought that I can host this solution in somewhere and view it inside an iframe or something.
Am I thinking right?,
is this the correct step to take in order to achieve this requirement inside Orchard?
to make it more clear, all I need to do is to view this solution and interact with it's controls and views from a hosting page inside orchard, and the subsequent requests should be handled by my solution in order to hit it's own data store and get back with the requested data in order to be displayed to the user.
any help would be appreciated.
Update:
thanks for Bertrand Le Roy for his answer, I can now view my solution inside my
orchard website.
I came in to one more HUGE problem, which is that my application can no longer connect to my external database.
I've a DB that is hosted in some where else, and I'm using EntityFramework to deal with it.
the problem is that if I put the connection string inside my module web.config, or main orchard web.config, I run into several types of errors like:
"System.Reflection.TargetException: Object does not match target type."
or
"System.Data.MetadataException: Unable to load the specified metadata resource."
My question is: How could I pass my connectionstring correctly to my solution, assuming that I'm using Entity framework as my ORM.
Many thanks.
You will need to put it into a module.
You will have to move route definitions to a Routes.cs file (look at any existing such file for examples).
You will also need, in order to access your data store, to opt out of the ambient Orchard transaction around the data access code (using (var scope = new TransactionScope(TransactionScopeOption.Suppress))).
If you are using dependency injection, you may have some work to move that to the Autofac-based way of doing things in Orchard.
If you want your work to appear seamlessly in the Orchard admin, you may want to decorate your admin controllers with the Admin attribute. If you want your front-end to use the current theme, you'll have to add Themed attributes and maybe refactor your views so that they only emit HTML for the content zone instead of for the whole page.
Add a manifest (module.txt) to your module folder and you should be good to go.
I have a WP7 project, which will invoke a REST web service in Azure (MVC4 WebApi).
My WP7 project has models that it serializes to JSON and then sends to the web service.
The web service deserializes the data sent from WP7 and instantiates the models again before saving them to Azure Table Storage.
How can I share the Model classes between the projects? Right now I'm just copying the cs files over, and I have to update both sets if I make a change to the models. I was hoping a simple class library project would be able to be referenced from both projects, but WP7 couldn't handle that.
What should I do?
Thanks!
There are many solutions for this issue:
You could use a T4 template to read the entity and generate a class your WP7 project that only contains the properties of the object without reference to the Table Storage specifics (like TableStorageEntity): http://weblogs.asp.net/cibrax/archive/2009/03/11/code-generation-with-t4-an-entities-to-dto-example.aspx
You could split your entity over 2 files, one with the TableStorage specifics like TableStorageEntity and one file containing only the properties of the entity (use partial classes for this). Then you can add the file containing only the properties in your WP7 project as a link.
Create a DTO (or whatever you call it) class manually and use something like AutoMapper to map between the DTO and the TableStorage entity. Store the DTO in a portable library so it can be used by every kind of project. In my opinion this is the best solution since you don't want to completely expose your entities to "the outside world". An example would be a list of users. You wouldn't want to return all fields including password, hash... and other sensitive info. It would be better to have a separate class that only contains the info you want to expose externally.
We are planning a rewrite and I want to get the latest tools to make sure we are doing things the best way. I like how code first keeps the DB structure in code.. which is already source controlled.. So I am hoping that would remove the need for a DB project.. Yes? No ?
DB projects also allow you to sync data between enviorments but that is something else that has nothing to do with code first
Using Entity Framework Code First you can write things out in C# code and have it create/persist the data. Since you can source control C# code, you can source control the database, but only the schema. You won't be able to 'source control' the data.
Personally, I don't use it because it forces you to do some wonky stuff such as declaring properties as virtual and using decorators all over the place. Not only that but you really on the framework to do a lot of things behind the scenes and when things go wrong that's never good.
The route I choose and that works very well for me is to declare a Project.Domain class library project with the Entity Framework generated model from an existing database.
Then I would reference this project from my Project.WebUI (my MVC3 project) and have access only to public repository classes.
So my project looks like: