在实体中创建非映射属性(实体框架)

ryu*_*ice 36 entity-framework

我想在我从数据库映射的一个实体上创建一个自定义属性,但是这个属性没有映射到数据库,我使用部分类创建了属性但是在尝试编译时我得到一个错误告诉我该属性不是映射.是否有属性或我应该添加的内容?提前致谢.

Kni*_*lke 102

您还可以使用[NotMapped]属性或使用Ignore方法从流畅的API 标记您的属性.

属性

public class EntityName
{
    [NotMapped]
    private string PropertyName { get; }
}
Run Code Online (Sandbox Code Playgroud)

流畅的API

 public class Entities : DbContext
 {
    public DbSet<EntityType> Users { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       // Some database configuration
       modelBuilder.Entity<EntityType>()
             .Ignore(i => i.PropertyName);
     }
 }
Run Code Online (Sandbox Code Playgroud)


Ori*_*ian 10

使用部分类添加要添加的属性或方法.例如

namespace WhateverNamespaceYourEntityModelIsIn
{
    public partial class  TheNameOfYourEntity
    {
          public string MyNewProperty { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

那应该是你.

  • 在Code First的情况下,这是错误的答案 (2认同)