-1

我有这些模型

clients(id,name,address)
cars(id,platenum)
client_cars(clientid,carid)

我想做的是添加cars到 aclient并将数据保存到client_cars表中。添加数据的表单将具有车牌号复选框。有谁知道实现这种想法的好例子?

4

3 回答 3

1

使用 EF,您的概念模型不应包含多对多表。它仅在商店模型中。您的对象应该看起来更像这样:

clients(id,name,address, cars[])
cars(id,platenum, clients[])

在 UI 中,您通常会有一个包含客户信息和汽车列表的客户表单。此外,您的汽车

于 2013-02-13T21:39:30.523 回答
0

我找到了这个例子,它回答了我的问题:http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework -in-an-asp-net-mvc-应用程序

于 2013-02-13T19:22:27.393 回答
0

您始终可以在 SimpleMembership 中使用该示例,该示例具有:

角色

[Table("webpages_Roles")]//3
public class Role
{
    public Role()
    {
        Members = new List<Membership>();
    }

    [Key]
    public int RoleId { get; set; }

    [StringLength(256)]
    public string RoleName { get; set; }

    public ICollection<Membership> Members { get; set; }
}

会员资格

[Table("webpages_Membership")]//1
public class Membership
{
    public Membership()
    {
        Roles = new List<Role>();
        OAuthMemberships = new List<OAuthMembership>();
    }

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

    public DateTime? CreateDate { get; set; }

    [StringLength(128)]
    public string ConfirmationToken { get; set; }

    public bool? IsConfirmed { get; set; }

    public DateTime? LastPasswordFailureDate { get; set; }

    public int PasswordFailuresSinceLastSuccess { get; set; }

    [Required, StringLength(128)]
    public string Password { get; set; }

    public DateTime? PasswordChangedDate { get; set; }

    [Required, StringLength(128)]
    public string PasswordSalt { get; set; }

    [StringLength(128)]
    public string PasswordVerificationToken { get; set; }

    public DateTime? PasswordVerificationTokenExpirationDate { get; set; }

    public ICollection<Role> Roles { get; set; }

    [ForeignKey("UserId")]
    public ICollection<OAuthMembership> OAuthMemberships { get; set; }
}

用户角色

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Membership>()
          .HasMany<Role>(r => r.Roles)
          .WithMany(u => u.Members)
          .Map(m =>
          {
              m.ToTable("webpages_UsersInRoles");
              m.MapLeftKey("UserId");
              m.MapRightKey("RoleId");
          });
    }

抱歉,我本可以简化代码,但我不想更改原始类的任何内容。但这几乎应该让您对如何做到这一点有所了解。

于 2013-02-13T21:52:44.067 回答