I'm working on a C# WPF app with a MySQL backend using Entity Data Model in VS2010 (.NET 4). I have the following simple test database schema (ignore StudentCourse.Grade for now):
Students are assigned to courses with a many-to-many relationship via a connecting table. In EDM this is represented as two entities linked by an association mapped to the connecting table:
So far so good.
Now let's say that each student gets a final grade for each course he/she takes which I've stored in the StudentCourse table as Grade. My problems is somehow getting the grade in the EDM. I've tried creating a new StudentCourse entity mapped to the StudentCourse table but it's set to read-only because it has no id. Adding an id to the table causes Visual Studio to whine because the id field is not mapped in the association(which I don't understand at all).
How is a relationship like this mapped in EDM? I'm open to changing the database schema if need be.
Edit in reply to Ucodia:
I don't know. As far as UI goes, grades should be easily accessible for a student or for a course. I'm open to suggestions.
You need to add Grade to the link table and delete the many-to-many relationship. Then regenerate the model from the database. You will find that the StudentCourse table will show in the EDM. When there are additional fields in the link table, the link table will appear in the model.
Why would you not just store the Grade on the Course entity?
Apparently your primary concern is to have something like the Model you designed in EDM designer. If only the object model matters to you, whatever the database looks like, then I would suggest you having a look at EF Code First.
Have a look at this article: EF Code First Walkthrough
Related
I have a database with some tables for school related project and I have a model with EF 6.0 SQL-first approach. I need to update the database with a new table & update an existing table with a new column. The twist is: I don’t have any *.edmx file.
How can I update the model without it? If it is impossible, then how can I generate *.edmx without interrupting the existing model?
Entities are essentially POCOs, so you really just need to update your schema and update the entity classes to match. For new entities if the project is not using an edmx then it should either be using classes extending EntityTypeConfiguration or setting things up with the modelBuilder on the OnModelCreating event in the DbContext.
EF can resolve most general mappings using convention, so adding a column to a table usually just means adding the property to the entity. Mapping only comes into play when you want to change a columns naming, handle type casting differences, or use identity/computed columns. For new entities it can also use convention, but commonly there would be config used for the Table name, PK name, and things like Identity columns, plus navigation properties for related entities.
I am learning Entity Framework to query the database of my company. I have an ASP.NET MVC project and as of now, I have established a connection to the company's principal server database. That has given me access to all the tables and I created a separate class Library containing all the corresponding POCOs(generated automatically).
In the tutorial I was following they say to use "enable-migrations" to have the database updated with the model.
So does that mean that if I were to modify the models in my project, that would have a direct effect on the database? Since I am new to this project I do not want to do anything stupid, like altering the database. For now I just want to query the database and retrieve information, then use that information to show more or less information on a web page.
EDIT: Just as an example, I would like to show a difference between the model generated by EF and what my real table looks like. I have a table Web_Profils that contain and ID, a ProfileName and an Order (int). This DB has no primary keys or foreign keys. If there are relations, they are defined through new tables. But when EF generates all my classes, it adds ICollections, for example in Web_Profils, I have a.o virtual ICollection<Web_User_joint_Profils>Web_User_joint_Profils which is not present in the actual table, it just seems to be the relation that EF has deduced(it is the relation between Users and Profiles present in the table Web_User_joint_Profils). Now, will doing a migration affect my tables just because EF has added these collections in my model?
I've also read that it is possible to deactivate migrations using :
Database.SetInitializer(new ContextInitializerNone<YourDbContext>());
Any thoughts?
If you update your model, you need to add a migration to your project and update your database with that migration.
Unless you do those steps after updating your model, changes will not be reflected in the database.
I have a very basic question about database programming, here's the problem:
I want to create/read/edit/etc.. data from database without using Entity Framework, and for this job I've chosen SqlFu.
I want to put the stored procedures to create, update, delete on the database and the views to get entities.
My doubt is: If I have an table Employee, that has a one-to-many relationship to Tasks table, when I create a Sql View to retrieve Employee entity, should I retrieve the data in Tasks table that is related to the employee?
If so, how to do that with a single View in SQL Server? If not, I should have different Sql Views that retrieve data from each table and bind the relationship in the application?
I'm a bit lost in this subject :S
No, you don't need. You can retrieve any data from any table/view without need to always retrieve data from any related table's.
On ORM layer it's should be implemented as lazy loading - like in EF. But not in MicroORM's like SQLFu - there you should manually do something like
employeeObject.Tasks = db.Query<Task>("select * from tasks where employeid=#0", employeeObject.Id)
When and if you actually need it.
Yes, you should retrieve data from each table and bind the relationship inside application code.
I have a database with two tables, customer and account. Because multiple customers can exist on multiple accounts this is a many to many design.
This is how I designed it in SQL
This works quite nicely as Entity Framework picks up that its a mapping table and just maps Customer to Account as lists on each and hiding the mapping table. Brilliant!
I would like to extend this further to add preferences to a mapping between a customer and an account like such:
Am I right in assuming this is not possible? I have tried adding it to EF model but instead it brings back the mapping table.
Anyone else had any luck with this?
What you have in your first case is a simple many to many relation table, which in Entity Framework results in the collections of entities on one another.
If you want to have a relation to the Preference from your CustomerAccount relation table, the relation becomes complex and it cannot be depicted in the simple relation lists anymore. You need add an entity for your relation CustomerAccount which will have foreign keys on Customer, Account and Preference.
Assumptions
Using EF 6.1, MVC 5, VS 2013, C#
I have an existing database model designed in Toad DM for SQL Server and it's very important keep it always updated
Steps and Notes
Using ADO.NET Entity Data Model I chose Code First from Database (new feature in EF 6.1) to generate the models. Note: Model classes and DbContext class generated successfuly but NO .edmx or .tt file was generated.
Next I added a new scaffold item: MVC 5 Controllers with views, using Entity Framework. Note: Success, controllers and views generated
Question
From now on I don't want to use Code First to update my database. Instead I want the models to be updated based on database changes. What to do next? If I don't have an edmx file will I not be able to update my model classes from the database?
The Entity Data Model Wizard's Code First from Database does an excellent job creating your entity classes, as if they were created in the Code First style. What you are asking is if there is any way to keep these classes up-to-date as your database changes, similar to the EDMX style "Update Model From Database". From what I've researched this is not possible using the built-in tooling. However, here is one workaround that I've found useful:
Let's say I have database with a product table and customer table. Originally I created a StoreDBContext class, and selected product as one of my objects. Now I want to add the customer table as a new entity to the existing context. Here's how to do this using the Code First Wizard:
Create a new Entity Data Model, call it StoreDBContextTemp or whatever
Choose the code first from database wizard option
Select customer as an object to add (just customer) & complete the wizard
Open the newly created context file, StoreDBContextTemp.cs, and copy the virtual properties of your newly added entities:
public virtual DbSet<Customer> Customers {get; set;}
Paste these new properties into your Original StoreDBContext.cs dbcontext class.
Delete StoreDBContextTemp.cs, and remove the connection string for StoreDBContextTemp in app.config/web.confg etc.
You can now use Customer on the StoreDBContext class
If you add or remove tables you will need to manually adjust fields, but at least you won't need to hand write dozens of properties each time a new table is added to the model.
One more option is just delete the auto generated classes from the project and once again generate them.
While following this approach only thing we need to make sure that is we should give the same name for the data model(class name which inherits from DbContext ) as the previous one.Data model name is highlighted in below snap
Three things.
There's no .edmx when you use Code First.
If you use Code First Migrations you would have to write first the code and after that migrate the changes to database. This helps you to have much more organized you code with no generated code which is an advantage.
There's a plugin in Visual Studio for doing contrary. Entity Framework PowerTools allows you to select the database and map it to objects.
https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
The best solution to me is deleting the model and recreate updated one with the same name, keeping in mind two points:
Personal extension methods implemented for the model;
Possible manual relationships between tables added to the model because of not setted up in the phisical db.
My personal solution:
Move all extension methods to another partial class that won't be overrided;
Insert all added properties of an entity to another partial class;
Keep track of all manual relationships in an help file, so you can add them again being sure not to loose anything;
Delete the old model and recreate one new with the same name and update it with the manual relationships of point 3.