0

我有两个实体类,ContactUser.

User具有ContactID作为外键的Contact. 我可以显示Userto的关系Contact

现在我还需要Contact与表User中的相同外键建立关系User(而不是Contact

public class Contact{
   public int ContactID {get;set;}
   // This relation doesn't work
   // Need to Make this Work
   public User ContactUser {get;set;}
}

public class User {
   public string Id {get;set;}
   public int? ContactID {get;set;}
   // This Relation works
   [ForeignKey("ContactID")]
   public Contact Contact {get;set;}
}

所以我需要让这种ContactUser关系继续下去Contact。我应该使用什么样的映射?


编辑

正如我所建议的那样: modelBuilder.Entity<Contact>().HasRequired(c => c.ContactUser).WithOptional(pi => pi.Contact); 我从中删除了ForeignKey属性User,它导致Multiplicity...error

但我得到错误喜欢: Invalid column name 'User_Id'.

4

2 回答 2

0

您正在寻找的是一个

一对零或一映射

StackOverflow上有一个很棒的答案。


在您的情况下,需要明确以下内容:

  • modelBuilder 需要 WithOptional-Methode

    .WithOptional(pi => pi.Contact);

  • 没有财产ContactID

EF 6.0 示例

public class Contact
{
    [Key]
    public int ContactId { get; set; }

    public string ContactName { get; set; }
    public User ContactUser { get; set; }
}

public class User
{
    [Key]
    public int UserId { get; set; }

    public string UserName { get; set; }
    public Contact Contact { get; set; }
}

在 DbContext 类中:

public class CodeFirstContext : DbContext
{
    public CodeFirstContext()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CodeFirstContext>());            
    }

    public DbSet<Contact> Contacts { get; set; }
    public DbSet<User> Users { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
          .HasRequired(co => co.Contact)
          .WithOptional(cu => cu.ContactUser);

        base.OnModelCreating(modelBuilder);
    }
}

在这种情况下,现在说用户的联系人属性是可选的,而联系人的用户属性是必需的。 我希望这是适合您项目的星座。

控制台测试

class Program
{
    static void Main(string[] args)
    {
        using (var db = new CodeFirstContext())
        {
            Console.Write("Enter a name: ");
            var name = Console.ReadLine();

            var cont = new Contact { ContactName = name };
            db.Contacts.Add(cont);
            db.SaveChanges();

            var usr = new User { UserName = "MyUsername", Contact = cont };
            db.Users.Add(usr);
            db.SaveChanges();

            var query = from b in db.Contacts
                        orderby b.ContactName
                        select b;

            Console.WriteLine("All contacts in the database:");
            foreach (var item in query)
            {
                Console.WriteLine(item.ContactName);
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}
于 2015-04-20T11:23:07.197 回答
0

试试这个:

public class Contact
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ContactId { get; set; }

    public string ContactName { get; set; }
    [ForeignKey("ContactId")]
    public User ContactUser { get; set; }
}
于 2015-04-20T13:28:56.623 回答