ef-code-first

Database Initialization in Entity Framework Code First

烂漫一生 提交于 2019-12-11 03:12:39
问题 I'm learning about EF Code First. I've read Database Initialization Strategies in Code-First and Understanding Database Initializers in Entity Framework Code First articles. But I'm still confused, where should be the data before initialization? Those articles didn't mention about that and I think it's pretty important. I'm building a football application, so while initializing app I'd like to insert every team and player name into database (every in the Europe from best leagues - so quite a

Entity Framework 4.1 Code First EDMX issue

眉间皱痕 提交于 2019-12-11 03:09:56
问题 The way I handle large projects using EF CF is by doing the following: Create a data model (EDMX) Disable the Model-First code generation feature for that model by clearing the Custom Tool property Use Entity Framework POCO Generator extension to add the relevant template (t4) files to the project Customize the template files to follow certain coding conventions we follow This all looks good in concept but when we run the project, EF seems to think we are using the Model-First approach. This

EFCodeFirst : The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

£可爱£侵袭症+ 提交于 2019-12-11 03:07:55
问题 I am trying to find out what is causing this error, I have listed some of the relevant areas of my code that should hopefully help answer my problem. The recipe entity's members collection is as shown below: public virtual IList<Member> Members { get; set; } and here is the Recipes collection on the member entity: public virtual IList<Recipe> Recipes { get; set; } I do the below when creating my DbContext in order to make a many-to-many relationship in a separate table protected override void

Ignore pending migrations after database recreation

佐手、 提交于 2019-12-11 03:07:42
问题 We am using code first EF 4.3.1 with database migrations. Sometimes we drop the database and let EF recreate it, mainly for local development purposes. There is a procedure which applies pending migrations and when the database is recreated the pending migrations aren't necessary and error is thrown when EF tries to apply them. I check the migration date from the string and if this is before the database creation date then I manually add it to the __migrationHistory table and don't apply the

EF 4 Code First: with existing tables

那年仲夏 提交于 2019-12-11 03:00:49
问题 I have an existing table in my database and I want to create some additional tables using Code First/Fluent API (whatever way you may want to categorize it). Following is my DbContext : public class MyContext : DbContext { public DbSet<Entity1> Entities1 { get; set; } public DbSet<Entity2> Entties2 { get; set; } public MyContext() : base(@"data source=....;") {} protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new MyContextInitializer());

How to use SQL Server Views with EF Code First Migrations

会有一股神秘感。 提交于 2019-12-11 02:57:47
问题 I've reverse engineered a small database that contains a number of tables and views into EF 5 code first. When I do this, I get a set of models and mapping classes for each of my tables and these work well when I try to query the database using the generated DbContext . It also generates POCO classes for each of my views, but when I run Enable-Migrations and Add-Migration using the Package Manager Console, the migration does not re-create the SQL Server views. I have tried updating the

EF code-first: Sequence contains more than one matching element

你离开我真会死。 提交于 2019-12-11 02:57:35
问题 I'm using EF6 code-first, and have a User class that needs to have incoming and outgoing connections to other users. A connection also has properties, so I also have a Connections class. public class User { public int ID { get; set; } // Other properties removed here to keep it simple [InverseProperty("SourceUser")] public virtual ICollection<Connection> OutgoingConnections { get; set; } [InverseProperty("DestUser")] public virtual ICollection<Connection> IncomingConnections { get; set; } }

EF 4.3 Code First Migrations - Seed per migration

試著忘記壹切 提交于 2019-12-11 02:52:29
问题 have recently started using Code First Migrations and was wanting to seed data in each Up method. is this possible? Ie. Create database table Fill with data for Drop: Delete data from joining table delete table 回答1: Yes it is possible but you must do it through executing SQL commands. Use Sql method in both Up and Down methods of your migration to execute INSERT and DELETE SQL commands. 回答2: public partial class DBInitializer : IDatabaseInitializer<DBContext> { public void InitializeDatabase

How can I create and use views using EF6 Code First?

自古美人都是妖i 提交于 2019-12-11 02:47:06
问题 Is there actually any official strategy for creating and using views with EF6.1 Code First? I can reverse the database into Code First and get tables not views. I can import the database into Model First and get views and then do Generate DB from Model and my edmx is modified and the views are converted to tables. MSFT seems to have all but abandoned Model First, and the new Update 2 seems to have reverse engineering for Code First so I feel forced to convert to Code First but has the EF team

DbEntityValidationException After Upgrading to EF 4.1

旧巷老猫 提交于 2019-12-11 02:37:40
问题 Today I decided to upgrade to EF 4.1 (from CTP5). I am using Code First. Unfortunately, whenever I try to run the project I get this exception: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. The EntityValidationErrors property doesn't really say anything about the entities that are causing the exception. Nether does the Stack Trace actually. But, the exception is thrown on the line