-1

I am new about MVC+EF

I need to insert data into 3 table at a time but can not Figure out how can I do this My data model is

public class UserLogin
    {
        [Key]public int UserID { get; set; }
        public string LoginName { get; set; }
        public byte Password { get; set; }
    }
    public class UserDetails
    {
        [Key]
        public int DetailID { get; set; }
        [ForeignKey("UserLogin")]
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string Address { get; set; }
    }
    public class UserType
    {
        [Key]
        public int TypeID { get; set; }
        [ForeignKey("UserLogin")]
        public int UserID { get; set; }
        public string TypeName { get; set; }
        public string TypeDiscription { get; set; }
    }
    public class UserBDCon : DbContext
    {
        public DbSet<UserLogin> logins { get; set; }
        public DbSet<UserDetails> Detail { get; set; }
        public DbSet<UserType> Type { get; set; }
    }

Can anyone tell me how does look the controller and view file for this model ?

4

2 に答える 2

0

モデルクラスに追加のプロパティを作成する必要があります。

そして、データモデルは次のようになります。

public class UserLogin
{
    [Key]
    public int UserID { get; set; }
    public string LoginName { get; set; }
    public byte Password { get; set; }
}
public class UserDetails
{
    [Key]
    public int DetailID { get; set; }

    [ForeignKey("UserLogin")]
    public int UserID { get; set; }

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

    //additional
    public UserLogin UserLogin { get; set; }
}
public class UserType
{
    [Key]
    public int TypeID { get; set; }
    [ForeignKey("UserLogin")]
    public int UserID { get; set; }
    public string TypeName { get; set; }
    public string TypeDiscription { get; set; }

    //additional
    public UserLogin UserLogin { get; set; }
}

例(作業単位とリポジトリはありませんが、単一のコンテキスト):

    UserBDCon db = new UserBDCon();

    var userLogin = new UserLogin() {LoginName = "Admin", Password = new byte()};
    var details = new UserDetails() {Address = "Address", UserName = "Admin", UserLogin = userLogin};
    var type = new UserType() {TypeDiscription = "Description", TypeName = "TypeName", UserLogin = userLogin};

    db.logins.Add(userLogin);
    db.Detail.Add(details);
    db.Type.Add(type);

    db.SaveChanges(); // EF insert data into 3 table at a time

同じdbコンテキストを使用する必要があります。その場合のクエリは同じトランザクションの一部になります。このhttp://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-のような目標の単一トランザクションのリポジトリパターンと作業単位をより適切に実装します。 unit-of-work-patterns-in-an-asp-net-mvc-application

于 2012-10-16T09:03:24.930 に答える
0

I must create new class which would contain all this classes something like this:

public class ViewModel()
{
   public IList<UserLogin> LoginModel {get; set;}
   public IList<UserDetails> DetailsModel {get; set;}
   public IList<UserType> TypeModel {get; set;}
}

And you must return this model return View(ViewModel) and create strongly tiped view than you can bind tables like this:

foreach(var item in ViewModel.LoginModel)
{

}
于 2012-10-16T07:59:50.610 に答える