我使用EF CodeFirst生成一个相当大的模式,包括TPT表,例如
[Table("MissingReplacementDongles")]
public class MissingReplacementDongle : ReplacementRentalDongle
{
public virtual bool OriginalStolen { get; set; }
}
public abstract class ReplacementRentalDongle : ConsequentialRentalDongle
{
public virtual Money ReplacementCharged { get; set; }
}
[Table("ConsequentialRentalDongles")]
public abstract class ConsequentialRentalDongle : RentalDongle
{
}
[Table("RentalDongles")]
public abstract class RentalDongle : BaseConstraint
{
public virtual DateTime CurrentExpirationDate { get; set; }
public virtual DateTime? ReturnedDate { get; set; }
}
我一直在针对SQL Server Express 2008安装进行开发,这完美地生成了表,即具有ReplacementCharged_Id列的ReplacementRentalDongles表,该列引用了Money表。
然而,当我运行相同的代码,但使用SQL Server 2008R2 Developer实例时,生成的表是不同的!在这种情况下,ReplacementCharged_Id列最终在"base"表BaseConstraint中生成。
这是EF 4.3.1中的一个错误吗?因为不同的SQL Server版本最终会创建不同的Schema?EF与SQL 2008R2兼容吗?
只是想让大家知道,我通过简单地升级到Entity Framework 5.0(两周前刚刚发布)就解决了这个问题http://blogs.msdn.com/b/adonet/archive/2012/08/15/ef5-released.aspx)
EF 5.0已经实现了一些新功能,这些功能在.NET 4.5上运行时是可用的(我们没有,我们在.NET 4.0上)。然而,EF 5也在.NET 4上运行,它似乎也是EF 4.3.1在.NET 4中运行时的错误修复汇总。
短版本-将EF DLL升级到5.0(通过NuGet),现在在SQL 2008 Express and 2008R2上为新数据库生成了相同的(正确!)模式,效果非常好!
一个小警告-我不得不用额外的using
指令更新我的源代码,因为[Table]
、[InverseProperty]
等属性已被移动到System.ComponentModel.DataAnnotations.Schema
命名空间(它们在EF 4.3.1中的System.ComponentModel.DataAnnotations
命名空间中)
简单的解决方案-只需升级!