2017-05-01 33 views
0

我有一個類(Employee)在我的DBContext中,我想通過導航屬性與另一個類(Office)加入。 Office類是來自外部源的東西。這是可以輕鬆完成的事嗎?實體框架 - 導航屬性外部源

public class Employee 
{ 
    [Key] 
    public int EmployeeId { get; set; } 

    public string OfficeCode { get; set; } 

    public virtual Office Office { get; set; } 
} 

public class Office 
{ 
    public string OfficeCode { get; set; } 

    public string Name { get; set; } 
} 

public class MyContext : DbContext 
{ 
    public virtual IDbSet<Employee> Employees { get; set; } 

    public MyContext() : base("name=constring") 
    { 

    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
    } 
} 

辦公室可以收集:

Office GetOfficeByOcd(string ocd); 

回答

1

你不能做到這一點...
由於辦公室是不是真的是你的數據庫模型的一部分,你不能有「外鍵「像OfficeCode這樣的屬性。 儘管可以阻止您在Employee類中添加GetOfficeByOcd方法。

這就是說,由於GetOfficeByOcd方法並不真正依賴於僱員它可能會更好,將其移動到更合適的地方(如辦公室類或OfficeService)

+0

這太糟糕了。謝謝你的澄清。 – DrivenTooFar