ef-code-first

Place to put Database.SetInitializer

走远了吗. 提交于 2019-12-17 07:28:58
问题 I'm working on a project that at can end up with multiple UI versions / variants, but so far I've got two subprojects in my solution Web - containing Web interface with ASP.NET MVC. Service project is place where I have my database context and models defined. My Goal is to have minimum or possibly no references to EF specific code in my Web project. I want it to be independent so when I switch the dlls with service backend ( from let say SQL to XML or MySQL ) I shouldn't make multiple

EF5 Code First - Changing A Column Type With Migrations

血红的双手。 提交于 2019-12-17 07:22:28
问题 I am new to EF5 Code First and I'm tinkering with a proof-of-concept before embarking on a project at work. I have initially created a model that looked something like public class Person { public int Id { get; set; } public string FirstName { get; set;} public string Surname {get;set;} public string Location {get;set;} } And I added a few records using a little MVC application I stuck on the top. Now I want to change the Location column to an enum, something like: public class Person {

DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID

天涯浪子 提交于 2019-12-17 07:14:52
问题 I have the following code (Database is SQL Server Compact 4.0): Dim competitor=context.Competitors.Find(id) When I profile this the Find method takes 300+ms to retrieve the competitor from a table of just 60 records. When I change the code to: Dim competitor=context.Competitors.SingleOrDefault(function(c) c.ID=id) Then the competitor is found in just 3 ms. The Competitor class: Public Class Competitor Implements IEquatable(Of Competitor) Public Sub New() CompetitionSubscriptions = New List(Of

Entity Framework Code First Lazy Loading

人走茶凉 提交于 2019-12-17 07:14:17
问题 I am having two object classes public class User { public Guid Id { get; set; } public string Name { get; set; } // Navigation public ICollection<Product> Products { get; set; } } public class Product { public Guid Id { get; set; } // Navigation public User User { get; set; } public Guid User_Id { get; set; } public string Name { get; set; } } When i load a user using dataContext, i get the list of Products being null (this is ok). If i add "virtual" keyword to Products list, public virtual

How to create a table corresponding to enum in EF6 Code First?

大憨熊 提交于 2019-12-17 07:03:50
问题 I've followed MSDN on how to handle enumerations in Code First for EF6. It worked, as supposed to but the field in the created table that refers to the enumerator is a simple int . I'd prefer a second table to be produced, the values of which would follow the definition of the enumerator in C# code. So, instead of only getting a table corresponding to Department in the example on MSDN, I'd also like to see a second table populated by the items from Faculty . public enum Faculty { Eng, Math,

How to create a table corresponding to enum in EF6 Code First?

半世苍凉 提交于 2019-12-17 07:03:28
问题 I've followed MSDN on how to handle enumerations in Code First for EF6. It worked, as supposed to but the field in the created table that refers to the enumerator is a simple int . I'd prefer a second table to be produced, the values of which would follow the definition of the enumerator in C# code. So, instead of only getting a table corresponding to Department in the example on MSDN, I'd also like to see a second table populated by the items from Faculty . public enum Faculty { Eng, Math,

Entity framework, problems updating related objects

*爱你&永不变心* 提交于 2019-12-17 07:01:17
问题 I am currently working on a project using the latest version of Entity Framework and I have come across an issue which I can not seem to solve. When it comes to updating existing objects, I can fairly easily update the object properties ok, until it comes to a property which is a reference to another class. In the below example I have a class called Foo, which stores various properties, with 2 of these being instances of other classes public class Foo { public int Id {get; set;} public string

Exclude a field/property from the database with Entity Framework 4 & Code-First

北慕城南 提交于 2019-12-17 06:37:13
问题 I will like to know that is there a way to exclude some fields from the database? For eg: public class Employee { public int Id { get; set; } public string Name { get; set; } public string FatherName { get; set; } public bool IsMale { get; set; } public bool IsMarried { get; set; } public string AddressAs { get; set; } } How can I exclude the AddressAs field from the database? 回答1: In the current version the only way to exclude a property is to explicitly map all the other columns: builder

Defining multiple Foreign Key for the Same table in Entity Framework Code First

拟墨画扇 提交于 2019-12-17 06:27:32
问题 I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as above an extra column is created named City_ID in the Student table after migration. Id there a mistake or how to define these FKs? Thanks in advance. Student: public class Student { public int ID { get; set; } public string Name { get; set; }

How to configure many to many relationship using entity framework fluent API

自闭症网瘾萝莉.ら 提交于 2019-12-17 05:07:11
问题 I'm trying to set up a many to many relationship in EF code first but the default conventions is getting it wrong. The following classes describes the relationship: class Product { public int Id { get; set; } public string Name { get; set; } } class Account { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } One Account can have many Products. However the EF conventions will create the DB tables as: Products Table -------