So I have a database and have created a model from it. Everything works just fine. But now I want to add more tables to my database and produce models from them.
Is that possible with EF Database first? Do I have to recreate all models for it to add new ones? All tables are of course from same database.
Example:
Let's say I have Table1, Table2, and Table3.
And by using DBContextGenerator in VS I've generated models from .edmx file. So I'd have Model1, Model2, and Model3 for corresponding database tables, and I'd like to add Table4 and Model4.
Do I have to recreate everything or is it possible to add somehow?
Open up your .edmx in design view.
Right click in there somewhere and choose "Update Model from Database".
On the "Add" tab, expand "Tables", and find your new database tables.
Check them, and click finish.
I've done this regularly as the DB changes and tables are added during development, it's been very reliable.
To generate the classes, do the following:
Right click on the .tt file of the solution explorer.
Click on the "Run custom tool".
Click on "OK" for the warning.
new classes will be generated.
Related
I'm wondering if this is even possible. I'm writing an app in MVC using ASP.NET Core and EF Core. For the most part I've been doing code-first migrations (that's all I know how to do, as yet) for my entities.
Someone added a table to the database I'm using, and rather than deleting their table and doing it the code-first way, I'd like to just bring their table over using a DB first migration.
Is that even possible to mix and match DB-first/code-first techniques?
If it is, how do I do it? I can't seem to find anything about bringing over just one table. Only migrating a whole database, which is not what I want.
Yes, it's possible. The steps are the following:
Right click on your Models folder.
Select Add -> Add new Item
In the Add New Item Window, select Data -> ADO.NET Entity Data Model. (This would create a new DbContext, but we going to merge the two DbContext) Click Add
In the Entity Data Model Wizard select Code First from database.
Select your connection String.
Click in no, exclude sensitive data...
Uncheck Save connection settings.
Click next
Select the new Table
Click finish
In this point VS would generate two files: a new DbContext and the model for the table.
Now open de new DbContext cut the DbSet of your model and paste in your original DBContext, too cut the content of the OnModelCreating method and paste at the end of the OnModelCreating method of your Original DbContext.
The final step is add a new Migration ignoring the changes.
For example:
Add-migration NewTableAdded -IgnoreChanges -verbose
Environment : EF 6, SQL 2012
Setup: Database First, LazyLoading disabled
The question might appear more generic but will try to explain it in the best possible way.
I have a large application using ASP.NET MVC and grouped the entity based on the logical functionality. Hence we built multiple EDMX files
There is scenario in which we have to use the similar entity in two EDMX file.
School has relation to Teachers and Students.In first EDMX file, i used school and Teachers.In Second EDMX file, i used school and students
But only one Entity class getting created. If i run the custom tool on second EDMX context file, then the entity(school.cs) on my first edmx getting disappeared and it appears on the second one..
Why this strange behaviour occurs?
Here is the code in my first EDMX file
As you see here, i m not accessing school entity and also i disabled Lazyloading. But it complains that it couldnt find school file. Note: Courses has navigation property to school. But i didnt include it here.. Why its occuring so?
var courses= DB.courses
.AsNoTracking()
.Select(e =>
new CourseDTO()
{
CourseID= e.CourseID,
Name= e.CourseName,
Desc= e.Desc,
isActive= e.isActive
})
.OrderBy(e => e.CourseID);
The problem is, I m able to include one entity in the EDMX file only..
In first EDMX, it has navigation property to Teachers
In second EDMX , it has navigation property related to Students. But only Entity file exists at a time.. With only one Entity file, the code breaks
Note: This is just sample..Not my original application
Thanks #GertArnold. Meanwhile, i tried to create folders and kept the EDMX file inside it. Means i created seperate folder for each logical group and then included edmx file inside it. This in turn made the edmx file entities have different name space(i mean the entity classes) and also it enabled to have the same entity across multiple EDMX files. It sounds to have resolved my problem.
I didnt try to have them included under different namespace.. The whole idea started when i realized that even though i have two EDMX files, the associated entities(.csfiles) are created in the same physical location. I tried to create sub folders and included the EDMX files. It resolved the problem and i found it is having different name space
:):)
oracle 10.4
devart dotConnect - 6.50...
MVC2 project - web page
User fills out form, then controller gets new entity, fills it out. saves database
System_id before saving = 0 (its an int/number - so no cannot be null)
Several other tables linked, so they have their own System_id as well.
When it gets saved to database, some trigger (there is a stored trigger for the table, which I only seam to understand as when system_id=null to be fired), a new Number is assigned for System_id.
This all worked fine.
Then I came along, and needed some updates.
Another field needed on this "main" table
(I have earlier, added columns to another table, with out issue)
Added column to this "main" table (restrict_to_me)
Now when it tries to save to database - it trys saving "system_id=0".
Linked tables, also make records with system_id=0
In entity framework designer - i can see the field system_id ENTITY_KEY=true and StoredGeneratedPatern=Idenity
So I can not see what I have done to stop somthing from working with the entity framework, except updating the entity framework.
Any direction much appricated
thanks
When you added the new field, did you drop the table and recreate it?
If you did that then you deleted the trigger at the same time. So when you recreate the table, you also need to recreate the trigger.
Try inserting data just using an SQL statement and see if the id is generated.
This was an Entity Framework issue, one many have already had.
Not every time, but some times, when updating the model, StoredGenereatedPattern being droped in a section.
http://www.ladislavmrnka.com/2011/03/the-bug-in-storegeneratedpattern-fixed-in-vs-2010-sp1/
When looking at fixes, did not understand that BOTH SSDL and CSDL parts stored in same text.
So look in upper part that it has StoredGenereated Pattern.
I have created mvc3 application.
I have one .edmx already created which is based on Db1 but now
I have created a view which is based on Database2 and I need to use this view inside my project.
For that I need to update my EF .edmx file.
but when I right click and select option Update model from Database
i'm only getting all tables , view ,sps fromDb1` its obvious
But as i need to use view which is fromDatabase2how can i add it into my model.edmx` file?
please help.
If two edmx want to merge then make partial class same for both edmx file (there will be two designer classes). Add another constructor and make it parametrized, for other edmx file. Parameter to identify which edmx want to load.
Add another class file in Business layer create object of edmx partial class in this class file, Under this class when ever you want to load whom so ever edmx file pass some argument in constructor of edmx partial class constructor to identify which connection needs to open.
Pass parameter in constructor of edmx designer class, based on decided page name (custom logic or table name; That edmx will get loaded.
In web config file multiple connection strings will available for multiple edmx file.
Entity Framework does not support mapping more than one database to one model/.edmx file (see See : unify two models (edmx) with visual studio 2010)
So you'd need to create a separate .edmx file/model for the other database, and reference each model with separate contexts. You'll need 2 connection strings in your projects as well.
One "hack" might be, for i.e. MS SQL to link these two servers and expose the data from other one on first one, i.e. via view. But I think it's manageable only for few tables. With huge models this will be pain. Other databases (Firebird, Oracle, ...) support this in similar way.
What I have done , created stored procedure in db A and accessed the db B through that SP , say select * from db2.table.then create a function import for that particular SP .
This approach works well if you have both databases on same server. In case these are on different servers you can create Linked Server on B to access A using the same stored procedure approach.
Using ctx As New Entity()
ctx.Database.Connection.ConnectionString = conString
End Using
I go in and add a new "linq to sql classes" in Visual Studio and then go and drag a table from Database Explorer to the new DBML and the name of the new class is no longer plural. What if I still want it to be plural? If I drag a table that isn't plural I get a bunch of compile errors about how there is already a type definition for all of the fields in the table I put on the dbml layout. If I then make the class name plural for the table that wasn't plural in the dbml layout by clicking on the name and changing it then no more compile errors.
You can single click on the tablename in the dbml table and rename it. Or you can select the table in the DBML and go to the properties window. There you can change the name, and the table it hooks up to.
One way to avoid the name conflict is to designate namespaces accordingly. For example, you can place your DBML file in a subfolder in your project and assign it something like DataAccess. Therefore, when you map to the Ling2Sql class, you'd do DataAccess.Customer and you can avoid the conflict with Customer, since it lives somewhere else.
As a tidbit, Linq2Sql by default makes tables non-plural. It's based off of a convention. For example, a table Customers has many customers. When you instantiate an object, you're looking at a single Customer, not the table. The object is essentially being mapped to a row in the Customers table, thus it becomes singular.
I just figured out what was happening. After the "move it to a new folder" suggestion I tried that and it worked. But it didn't make any sense as to why it worked because if there were type conflicts there should have still been the same conflicts in the new folder but I had none of the problems I was having before and I could rename things anything I wanted after moving to a new folder and there were no conflicts. Renaming things after dragging it over is a bad idea because if the database schema changes and you want to update your classes you have to drag the table back over and then make the changes manually again.
ANYWAY, the problem is that before I made the "linq to sql classes" I made a "ADO.NET Entity Data Model". That's what was causing all the name conflicts. But the suggestion about moving it to a new folder was awesome and led me to the final solution!