C# 帮助为具有不同实体引用的实体创建Windows窗体

C# 帮助为具有不同实体引用的实体创建Windows窗体,c#,.net,winforms,entity-framework,ado.net,C#,.net,Winforms,Entity Framework,Ado.net,下面是场景(ADO.NET实体框架和C#) 联系人*: 地址*: **这不是真正的代码,但你明白了* 我正在尝试创建一个Windows窗体,它对用户来说是平面的。换句话说,表单将有四个字段(name、addr、street、city),当用户单击bindingSourceNavigator中的Add按钮时,我想创建一个新的Contact实例,以便Contact.addr是对新创建的地址的引用 如果我只处理对象,这将很简单,但我正在尝试在表中创建一个支持地址的新行 以下是我到目前为止所做的尝试:

下面是场景(ADO.NET实体框架和C#)

联系人*: 地址*: **这不是真正的代码,但你明白了*

我正在尝试创建一个Windows窗体,它对用户来说是平面的。换句话说,表单将有四个字段(name、addr、street、city),当用户单击bindingSourceNavigator中的Add按钮时,我想创建一个新的
Contact
实例,以便
Contact.addr
是对新创建的
地址的引用

如果我只处理对象,这将很简单,但我正在尝试在表中创建一个支持
地址的新行

以下是我到目前为止所做的尝试:

private void contactBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
    Contact newContact = new Contact();
    Address newContactAddr = new Address();
    newContact.Address = newContactAddr;

    newContactAddr.Contacts.Add(newContact);
    //I realize I don't need the Contact list reference in Address, 
    //but VS2010 created it, so I'm just adding the new Contact to
    //the list for now.

    e.NewObject = newContact;
}
private void contactBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    contactBindingSource.EndEdit();
    context.SaveChanges();  //throws UpdateException
}
一些背景:表单有一个
联系人
的绑定源,此方法是创建新的
联系人
时的事件处理程序。我在MSDN上读到,这就是在将对象实际添加到BindingSource之前修改对象的方式<代码>上下文
指的是我的实体模型

发生的情况:当我单击添加按钮时,我可以输入联系人信息。但是当我点击save按钮时,我会得到一个
UpdateException
。我怀疑这是因为我没有正确地创建
地址,但是对于ADO.NET框架(以及一般的.NET编程)来说是新手,我真的不知道正确的方法。

示例: 我有3个表,用户、用户角色和角色

当我创建一个用户时,我希望这个用户接收一个角色,然后我会这样做

using(DatabaseEntities db = new DatabaseEntities())
{
    //creates the user and add the properties except roles
    Users user = new Users();
    user.username = "Test";

    //get an existing role
    var role = db.Roles.SingleOrDefault(r => r.roleName == "User");

    //adds the userid and roleid in to userRoles
    user.Roles.Add(role);

    db.Users.AddObject(user);

    //saves it to the db
    db.SaveChanges();
}
因此,为了让它在您的示例中工作,您首先需要在使用它之前将其中一个对象插入db,以便将另一个对象连同行一起保存到将它们链接在一起的表中

我希望这个简单的例子能帮助你

private void contactBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
    Contact newContact = new Contact();
    Address newContactAddr = new Address();
    newContact.Address = newContactAddr;

    newContactAddr.Contacts.Add(newContact);
    //I realize I don't need the Contact list reference in Address, 
    //but VS2010 created it, so I'm just adding the new Contact to
    //the list for now.

    e.NewObject = newContact;
}
private void contactBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    contactBindingSource.EndEdit();
    context.SaveChanges();  //throws UpdateException
}
using(DatabaseEntities db = new DatabaseEntities())
{
    //creates the user and add the properties except roles
    Users user = new Users();
    user.username = "Test";

    //get an existing role
    var role = db.Roles.SingleOrDefault(r => r.roleName == "User");

    //adds the userid and roleid in to userRoles
    user.Roles.Add(role);

    db.Users.AddObject(user);

    //saves it to the db
    db.SaveChanges();
}