Tag: ef code first

是否支持导航属性的inheritance?

难以找到相关的搜索结果…… 鉴于此模型: public abstract class A { public int ID { get; set; } public int CustomerID { get; set; } public virtual Customer Customer { get; set; } } public class B : A { } public class C : A { } public class Customer { public int ID { get; set; } […]

如果可能,将if条件合并到单个LINQ语句中

我目前正在使用ASP.NET MVC和Entity Framework Code First使用存储库模式来显示具有分页的简单用户列表。 我添加了过滤该列表的function,允许最终用户从两个下拉菜单和两个带开始和结束日期的文本框中选择条件。 有没有比下面的代码更好的方法来执行此操作,我必须测试所有可能的参数组合,以便编写适当的LINQ查询: public IEnumerable GetAllByCondition(int? condition1, int? condition2, DateTime? startDate, DateTime? endDate) { if (condition1.HasValue && startDate.HasValue && endDate.HasValue) { return Database.Set().Where(x => x.Condition1 == condition1.Value && x.Date > startDate.Value && x.Date <= endDate.Value).ToList(); } if (condition1.HasValue && condition2.HasValue) { return Database.Set().Where(x => x.Condition1 == condition1.Value && x.Condition2 == condition2.Value).ToList(); […]

从Entity Framework 4.1 Code First中的NotMapped类派生实体类

我需要从不属于模型的基类派生我的两个Entity类。 [NotMapped] public abstract class BaseClass { [NotMapped] public string SomeProperty { get; set; } } public partial class Derived1: BaseClass {} public partial class Derived2: BaseClass {} 我已经尝试将基类及其所有属性标记为[NotMapped]但是上下文初始化器会抛出一个错误,指出我的派生实体类都没有映射。

如何使用代码优先CTP5将列映射到EF4中的复杂类型?

在entity framework4中遇到了缺少enum属性支持的问题,我发现这篇文章描述了一个解决方法: Entity Framework 4.0 Beta 1 – POCO Enum支持? 如何在代码中而不是在设计器中表示数据库列到OrderStatusWrapper (如文章中所述)的映射? 更新: 根据提供的答案,我没有意识到我需要用OrderStatusWrapper类型替换OrderStatus枚举,即 代替: public class Order { public OrderStatus Status { get; set; } } 我应该用: public class Order { public OrderStatusWrapper Status { get; set; } } 这让我更进一步,但是在执行以下代码时: // OrderContext class defined elsewhere as: // public class OrderContext : DbContext // { […]

entity framework一对多TPH映射

我正在使用类似于此的数据结构,其中动物的类型是从表中的鉴别器列确定的: public class Farm { public int Id { get; set; } public virtual ICollection Pigs { get; set; } public virtual ICollection Cows { get; set; } } public class Animal { public int Id { get; set; } public int FarmId? { get; set; } public virtual Farm Farm { get; set; } […]

如何观察DbSet 的Add动作?

我有两个名为Contact和ContactField类,如下所示。 将ContactField添加到Contact ,我希望自动将SortOrder分配给ContactField 。 我是否需要inheritanceDbSet并自定义Add方法? 怎么实现呢? public class Foo { private MyDbContext _db = new MyDbContext(); public void HelloWorld() { Contact contact = ….; //< A contact from database. ContactField field = ….; ///< A new field …. ///< assign other properties into this `field` field.FieldType = FieldType.Phone; // How to automatically update `SortOrder` // […]

虽然密钥是使用HasKey定义的,但“EntityType没有键定义”exception

使用EF 5(首先是逆向工程代码),我的模型工作正常,直到它突然停止。 \ tSystem.Data.Entity.Edm.EdmEntityType :: EntityType’Projects.Date’没有定义键。 定义此EntityType的键。 \ tSystem.Data.Entity.Edm.EdmEntityType :: EntityType’ProjectstRisk’没有定义键。 定义此EntityType的键。 我使用流畅的API而不是属性来定义键,这里是我的ProjectsDates类。 public partial class ProjectsDate { public string OSProjectCode { get; set; } public Nullable TargetStart { get; set; } public Nullable EndDateOriginal { get; set; } public Nullable EndDateChangeControl { get; set; } public Nullable EndDateActual { get; set; } public Nullable […]

无法更新Entity Framework中的多对多关系

我正在使用Entity Framework 4.3 Code First,我在更新多对多关系方面遇到了问题。 我定义了以下类: public abstract class Entity { [Column(Order = 0)] [Key] public int Id { get; set; } 1541642903 [Column(Order = 1)] public byte[] Version { get; set; } } public class Video : Entity { public string Title { get; set; } public string Description { get; set; } public […]

将entity framework从MVC移动到另一个项目会导致重新迁移

我目前有一个asp.net MVC 4应用程序,它包含Entity框架6 Code First模型,DbContext和Migrations。 为了将它与我的Web应用程序分开,以便我可以在另一个项目中重用这些数据库类,我已将所有相关的Entity Framework类移动到它们自己的项目中。 但是现在当我运行解决方案时,它认为我的模型已经更改并尝试再次运行我的所有迁移。 问题似乎在于我使用SetInitializer ,好像我注释掉了这一行,我可以正常运行Web应用程序。 public static class DatabaseConfig { public static void Initialize() { System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion()); // make sure the database is created before SimpleMembership is initialised using (var db = new G5DataContext()) db.Database.Initialize(true); } } 在我尝试移动所有Entity Framework类之前,这不是问题。 这是不可能的,还是我做了一些根本错误的事情?

使用EF的LINQ查询中的奇怪空引用exception

我有以下三个相关的实体类: public class ContextInstance { public Int64 Id { get; set; } public virtual List ContextParamValues { get; set; } } public class ContextParamValue { public Int64 Id { get; set; } public virtual Int64 ContextParamId { get; set; } public virtual ContextParam ContextParam { get; set; } public virtual ContextInstance ContextInstance { get; set; […]