插入桥表实体框架

时间:2012-05-31 16:48:36

标签: asp.net visual-studio entity-framework entity-relationship junction-table

嗨,大家好
我正在学习使用EF进行攀爬,我对EF有基本的CRUD理解,但现在我有一个具有导航属性的表(我怀疑是桥表),所以我需要在桥表中添加值,我想我可以用导航财产来做。

问题解释:
原始部分DB图 enter image description here

部分EF模型图
enter image description here

我写的代码:

   protected void BtnAddUser_Click(object sender, EventArgs e)
    {
        DBEntities entities = new DBEntities();
        var usr = new User();
        //I thought I would add an Roles object into usr.UserRoles.Add(usrRoles);
        //but UserRoles have only two fields ,RoleTypeId and UserId 
        //var usrRoles = new Roles() 
        //{Id=0,RoleDescription="dfdfdf",RoleType="WebSite Admin"}; 

        usr.UserName = TxtbxUserName.Text;
        usr.Password = TxtBxPassword.Text;
        usr.Email = TxtbxEmail.Text;
        usr.CreateDate = DateTime.Now;
        usr.LastActivityDate = DateTime.Now;
        usr.IsEnabled = true;

        //What to Add in the .Add method
        usr.UserRoles.Add(

        entities.User.AddObject(usr);
        int result = entities.SaveChanges();
        LblMsg.Text = result == 1 ? "User created successfully." : "An error occured    ,please try later.";

        entities.Dispose();
    }

更新(到目前为止我尝试过的):
我从角色表中获取“网站管理员”角色并将其放入ObjectContext.UserRoles.Add(UserRoleWebsiteAdmin); 这就是我在代码中所做的,

        //Fetch WebsiteAdmin from Roles
        var userRole = from usrRole in entities.Roles
                       where usrRole.Id == 1
                       select usrRole;

        usr.UserName = TxtbxUserName.Text;
        //same old code of usr.Property = someTextBox
        //I have tried to type cast it LinqtoEntities result into Roles
        usr.UserRoles.Add((Roles)userRole);

生成异常
enter image description here

P.S:如果您需要更多说明,请与我们联系。

2 个答案:

答案 0 :(得分:1)

非常感谢你们...... 在这里我做了什么,它的工作原理,

       DBEntities entities = new DBEntities();
       //Make user object
       var usr = new User();
       //Fetch type of Role from Roles table
       var userRole = (from usrRole in entities.Roles
                       where usrRole.Id == 1
                       select usrRole).Single();
        //copy user related info from textboxes
        usr.UserName = TxtbxUserName.Text;
        usr.Password = TxtBxPassword.Text;
        usr.Email = TxtbxEmail.Text;
        usr.CreateDate = DateTime.Now;
        usr.LastActivityDate = DateTime.Now;
        usr.IsEnabled = true;


        usr.UserRoles.Add(userRole as Roles);

        entities.User.AddObject(usr);
        int result = entities.SaveChanges();
        LblMsg.Text = result == 2 ? "User created succesfully." : "An error occured ,please try later.";

        entities.Dispose();

答案 1 :(得分:1)

也许您可以使用using http://msdn.microsoft.com/en-us/library/yh598w02.aspx和对象初始值设定项http://msdn.microsoft.com/en-us/library/bb384062.aspx来提高可读性:

using(DBEntities entities = new DBEntities())
{
       //Make user object
       var user = new User{
            UserName = TxtbxUserName.Text,
            Password = TxtBxPassword.Text,
            Email = TxtbxEmail.Text,
            CreateDate = DateTime.Now,
            LastActivityDate = DateTime.Now,
            IsEnabled = true
        };

       //Fetch type of Role from Roles table
       var userRole = entities.Roles.Where(x=>x.usrRole.Id ==1).Single();
       user.UserRoles.Add(userRole);

       entities.User.AddObject(user);
       int result = entities.SaveChanges();
       LblMsg.Text = result == 2 ? "User created succesfully." : "An error occured ,please try later.";
}

此致