programing

Entity Framework 4.1 코드 먼저 클래스 속성 무시

subpage 2023. 5. 24. 22:08
반응형

Entity Framework 4.1 코드 먼저 클래스 속성 무시

제가 이해하는 바로는[NotMapped]속성은 현재 CTP에 있는 EF 5까지 사용할 수 없기 때문에 생산에 사용할 수 없습니다.

EF 4.1의 속성을 무시하도록 표시하려면 어떻게 해야 합니까?

업데이트: 다른 이상한 점을 발견했습니다.알겠습니다.[NotMapped]EF 4.1은 어떤 이유로 데이터베이스에 폐기라는 이름의 열을 생성합니다.public bool Disposed { get; private set; }로 표시됩니다.[NotMapped]클래스가 구현합니다.IDisposeable물론이죠. 하지만 그게 어떻게 중요한지 모르겠어요.무슨 생각 있어요?

사용할 수 있습니다.NotMappedCode-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; }
}

[NotMapped]속성은 다음에 포함됩니다.System.ComponentModel.DataAnnotations 네임스페이스입니다.

또는 이 작업을 수행할 수 있습니다.Fluent API최우선의OnModelCreating의 기능DBContext클래스:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<Customer>().Ignore(t => t.LastName);
   base.OnModelCreating(modelBuilder);
}

http://msdn.microsoft.com/en-us/library/hh295847(v=vs.103).aspx

제가 확인한 버전은EF 4.3NuGet을 사용할 때 사용할 수 있는 최신 안정 버전입니다.


편집 : 2017년 9월

As.NET 코어(2.0)

데이터 주석

만약 당신이 asp.net core (이 글을 쓸 당시 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; }
}

Fluent 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; }
}

EF 5.0부터는 다음과 같은 기능을 포함해야 합니다.System.ComponentModel.DataAnnotations.Schema네임스페이스입니다.

언급URL : https://stackoverflow.com/questions/10385248/ignoring-a-class-property-in-entity-framework-4-1-code-first

반응형