2012-03-29 42 views
6

Tôi có một thực thể trong khuôn khổ luật Đầu tiên Entity hiện trông như thế này:Entity Framework - Tái sử dụng Complex Loại

public class Entity 
{ 
    // snip ... 

    public string OriginalDepartment { get; set; } 
    public string OriginalQueue { get; set; } 

    public string CurrentDepartment { get; set; } 
    public string CurrentQueue { get; set; } 
} 

Tôi muốn tạo Loại Complex cho các loại như một cái gì đó như thế này:

public class Location 
{ 
    public string Department { get; set; } 
    public string Queue { get; set; } 
} 

tôi muốn sử dụng cùng loại này cho cả hiện tại và gốc:

public Location Original { get; set; } 
public Location Current { get; set; } 

đây có phải là có thể, hoặc làm Tôi cần tạo hai loại phức tạp CurrentLocationOriginalLocation?

public class OriginalLocation 
{ 
    public string Department { get; set; } 
    public string Queue { get; set; } 
} 

public class CurrentLocation 
{ 
    public string Department { get; set; } 
    public string Queue { get; set; } 
} 

Trả lời

7

Nó được hỗ trợ ngoài hộp, bạn không cần phải tạo hai loại phức tạp.

Bạn cũng có thể cấu hình các loại phức tạp của bạn explicitely với xây dựng mô hình

modelBuilder.ComplexType<Location>(); 

Để tùy chỉnh tên cột, bạn nên cấu hình chúng từ cấu hình công ty mẹ

public class Location 
{ 
    public string Department { get; set; } 
    public string Queue { get; set; } 
} 

public class MyEntity 
{ 
    public int Id { get; set; } 
    public Location Original { get; set; } 
    public Location Current { get; set; } 
} 

public class MyDbContext : DbContext 
{ 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.ComplexType<Location>(); 

     modelBuilder.Entity<MyEntity>().Property(x => x.Current.Queue).HasColumnName("myCustomColumnName"); 
    } 
} 

này sẽ vạch MyEntity.Current.Queue-myCustomName cột

+0

Tôi đoán tôi không chắc cách nó được hỗ trợ ra khỏi hộp. Lớp 'ComplexTypeConfiguration ' có phương thức 'Property()' yêu cầu tôi chỉ định tên cột. Tên cột sẽ khác nhau cho mỗi số – Dismissile

+0

Tôi đoán tôi nên làm rõ rằng tôi muốn có thể tùy chỉnh tên cột cho cả hai loại phức tạp. Được hỗ trợ? – Dismissile

+0

Bạn có muốn chúng chỉ được thêm tiền tố khác hoặc đã được tùy chỉnh hoàn toàn không? – archil

Các vấn đề liên quan