使用2个不同数据库视图的导航属性

时间:2012-09-11 15:19:49

标签: c# linq entity-framework linq-to-entities

我有一个帐户对象,其中包含" Id" field,映射到数据库中的View:

public class Account : GeneralInfo
    {
        [Column("first_name")]
        public string FirstName { get; set; }

        [Column("last_name")]
        public string LastName { get; set; }

        public string Designation { get; set; }

        [Column("full_name")]
        public string FullName { get; set; }

        public string Email { get; set; }

        [Column("member_type")]
        public string MemberType { get; set; }

        [Column("status")]
        public string Status { get; set; }

        [Column("paid_thru")]
        public DateTime? PaidThru { get; set; }

        [Column("member_record")]
        public bool MemberRecord { get; set; }

        [Column("category")]
        public string Category { get; set; }

        public virtual Subscription Subscriptions { get; set; }
    }

我还有一个Subscription对象使用相同的" Id"作为帐户对象:

[Table("Subscriptions")]
public class Subscription
{
    [Column("Id")]
    public string ID { get; set; }

    [Column("Balance")]
    public decimal Balance { get; set; }

}

当我尝试使用"订阅"作为帐户的导航属性,我收到错误消息:{"Invalid column name 'Subscriptions_ID'."}

如何访问"订阅"使用Account对象?

1 个答案:

答案 0 :(得分:1)

使用fluent API映射Shared PK relationaship。

public class MyContext : DbContext
{
    // ...........

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Subscription>().HasRequired()
           .WithOptional(a => a.Subscriptions);
    }
}