简体   繁体   English

EF代码优先迁移 - 忽略属性

[英]EF Code-First Migration - Ignore Property

I have a simple poco class that has an enum property (Needed so that I can still have the code first create the enum lookup table). 我有一个简单的poco类,它有一个枚举属性(需要,所以我仍然可以让代码首先创建枚举查找表)。 I don't want the migration generator to add this column to the database. 我不希望迁移生成器将此列添加到数据库。 Is there an attribute or some other way to let the migration code know to ignore the property? 是否有属性或其他方式让迁移代码知道忽略该属性?

Example: 例:

public class MyPoco
{
    public int MyPocoId { get; set; }
    public int MyPocoTypeId { get; set; }

    public MyPocoTypeEnum MyPocoTypeEnum
    {
        get { return (MyPocoTypeEnum)MyPocoTypeId; }
        set { MyPocoTypeId = (int)value; }
    }
}

You can use NotMappedAttribute 您可以使用NotMappedAttribute

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.notmappedattribute(v=vs.103).aspx http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.notmappedattribute(v=vs.103).aspx

Or I prefer to use fluent mapping as it doesn't clutter my domain model with data access concerns. 或者我更喜欢使用流畅的映射,因为它不会使我的域模型与数据访问问题混乱。

modelBuilder.Entity<MyPoco>().Ignore(p => p.MyPocoTypeEnum); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM