Rah*_*han 291 .net c# entity-framework ef-code-first entity-framework-4.1
我的理解是,在[NotMapped]EF 5当前处于CTP中之前,该属性不可用,因此我们无法在生产中使用它.
如何将EF 4.1中的属性标记为忽略?
更新:我注意到其他一些奇怪的事情.我得到了该[NotMapped]属性,但由于某种原因,即使public bool Disposed { get; private set; }标记为,EF 4.1仍会在数据库中创建一个名为Disposed的列[NotMapped].该IDisposeable课程当然是实现的,但我不知道这应该如何重要.有什么想法吗?
Shy*_*yju 569
您可以使用NotMapped属性数据批注来指示Code-First排除特定属性
public class Customer
{
public int CustomerID { set; get; }
public string FirstName { set; get; }
public string LastName{ set; get; }
[NotMapped]
public int Age { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
[NotMapped]属性包含在System.ComponentModel.DataAnnotations命名空间中.
您也可以在课程中使用Fluent API覆盖OnModelCreating功能执行此操作DBContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(t => t.LastName);
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
http://msdn.microsoft.com/en-us/library/hh295847(v=vs.103).aspx
我检查的版本是 EF 4.3,当您使用NuGet时,这是最新的稳定版本.
编辑:2017年9月
数据注释
如果您使用的是asp.net核心(在撰写本文时为2.0), [NotMapped]则可以在属性级别使用该属性.
public class Customer
{
public int Id { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
[NotMapped]
public int FullName { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
流畅的API
public class SchoolContext : DbContext
{
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(t => t.FullName);
base.OnModelCreating(modelBuilder);
}
public DbSet<Customer> Customers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*ane 33
从EF 5.0开始,您需要包含System.ComponentModel.DataAnnotations.Schema命名空间.
| 归档时间: |
|
| 查看次数: |
189927 次 |
| 最近记录: |