I am working in an asp.net MVC 4 application and I am using Entity framework 6 in my application. I am using both code first approach for new tables/entities as well as database first approach with model designer(edmx). Customers are in codefirst model with separate context where as vehicles are in edmx. both have different context object. I want to use a query like this:
return View(maindb.Reservations.Include("customer").Include("Vehicle"));
but it returns error:
A specified Include path is not valid. The EntityType 'myproject.Data.Reservation' does not declare a navigation property with the name 'Vehicle'.
Please suggest how to fix it so that I can get properties of Vehicle and use them in my view.
I am using both code first approach for new tables/entities as well
as database first approach with model designer(edmx)
Wrong way to go.
Please suggest how to fix it
Put those entities into the same context.
You need to choose which approach is more suitable for you. For example, code first works as well with existing tables. If your tables don't match the naming conventions, you can easily override them with data annotations or fluent API, which is very flexible (see http://msdn.microsoft.com/en-us/data/jj591617 and http://msdn.microsoft.com/en-us/data/jj591620).
Related
I'm working on a sample app that exposes a restful API. I'm also using EF core to map entities mkd to a SQLite db.
Each entity has an ID property that is auto-generated and is unique.
The api expose a method for the creation of an entity by providing a model object. At the moment I'm using the same model that is mapped to EF Core (with the ID field included).
I honestly don't think that exposing the ID field during the creation of an entity is a good practice, so I thought to create another model without the ID field that will be passed to the create method and then map it the model that EF is using.
Is it a good practice or is it too overkill?
Am I missing something?
What you are essentially doing is the use of a DTO/ViewModel instead of the model object directly, and that is considered a good practice. It saves you from the problem of over-posting as well. Alternatively, you can choose to Bind specific properties as well - refer to https://www.hanselman.com/blog/ASPNETOverpostingMassAssignmentModelBindingSecurity.aspx
I have been using the Entity Framework, database first design for sometime now which means I've got many references to hundreds of Entity models in code that have been imported based on the name of the database table, of which some are singular and others are plural.
I would like to use the built in function that Entity allows when importing models to automatically make these models singular or plural in code regardless of the database table name however that function only seems to be work when adding NEW models.
QUESTION:
I don't want to have to delete all of my Entity Models and re-import because then my code wouldn't match up to a model without manually changing the many references to those objects. Does anyone know of a way to do this?
If anyone is unsure of what I'm referring to:
Entity Framework does not provide this refactoring functionality.
I ended up removing all models and reading them into my project with the proper naming scheme and then manually refactored all references to the models in code.
I am new to Entity Framework. I have created an EF Model and successfully added some tables and relation. Then I Clicked Generate Database from Model and My DB has been updated. Then I renamed some columns and I don't know how to revert or apply the changes. And Update Model from database does not seems to work because the columns names are different yet.
I need to graphically sync DB with Model. I prefer the model data rather than db data.
Thanks in advance.
You might want to look into the Code-First approach of Entity Framework. Using that approach you'll define your model in your code, and when changing anything you can create a Migration which allows you to up- and down-grade the DB to a specific version from the package manager console (or just create the respective SQL scripts).
For more information on this subject please see this article on MSDN
Note that you can also reverse engineer the code first model from an existing database (see 3. Reverse Engineer Model in this MSDN article), and then enable migrations for that model (see Step 2: Enable Migrations in this MSDN article)
What I do when I make "updates" is do it on both sides manually, in db and then in model (by right click properties) if the change is small. If adding a "new" table I drag it over to model from db server connections panel.
The alternative I've seen others prefer to use in this cases is to stay away from Entity Framework and use Dapper where you pass queries to it and it handles the rest.
Dapper (Wins!) vs Entity Framework vs ADO.NET Performance Benchmarking
I have a pre-existing database and Entity Framework 6 database-first model generated from it. I need to create the same database on a different host and/or database engine. I hoped I could use the same model as code-first, but OnModelCreating method of the generated DbContext seems to be protected from being used for code-first approach, it throws UnintentionalCodeFirstException. Generated models don't have any attributes applied too.
Is there any way to reuse my database-first model to create a database with the same schema? Or maybe there's an obvious and much simpler way to do it that I'm missing?
If you just want to create the database you shouldn't need to touch the OnModelCreating method. You should be able to do this from the designer (right click on the designer surface and select "Generate Database From Model" and run the script) or if you want to do this from code do ctx.Database.Create() or use the CreateDatabaseIfNotExists initializer.
I have used EntitFramework before to do codefirst with MVC; however, I have never reverse engineered an existing database to get my models.
So what I'd like to do is reverse engineer the existing DB then switch to codefirst so that changes made to the models will be reflected in the database automagically by the Entity Framework gods. How would I do this, specifically the reverse engineer then the switch to codefirst? Please have mercy if this is poorly worded or w/e I'm very new to MVC still.
It basically boils down to using tools to autogenerate the POCO classes that represent your domain, then using those classes as the basis for EF Code First and future migrations.
Here's a msdn article outlining the process
Code First to an Existing Database
You can reverse an existing Database by creating an Entity Data Model.
Check the following link. Here is described step by step the how to reverse a database to a CodeFirst approach with all of the navigation properties and required relationships. This scenario is also very useful for those entity relations that CodeFirst cannot resolve by its own and the configuration in not very intuitive.
Reverse from ModelFirst to CodeFirst