MVC 3首先使用代码(脚手架不起作用)

时间:2011-08-10 14:08:54

标签: ef-code-first

当我使用脚手架创建视图时,我生成的视图中没有一个属性。该属性应该充当我的LOV(就像prod的category属性一样)..查看链接http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx

继承我的实体。

public  class Member
{

    public string ID { get; set; }
    public string Name { get; set; }
    public System.DateTime DateApplied { get; set; }
    public System.DateTime? DateMembered { get; set; }
}

public class MemberType
{

    public string ID { get; set; }
    public string Description { get; set; }

}

public class BIMember : Member
{

    public new string ID { get; set; }
    [DisplayName("Name/Business Name")]
    public new string Name { get; set; }
    public virtual BIMemberType MemberType { get; set; }
}

public class BIMemberType : MemberType
{

    public new string ID { get; set; }
    public virtual ICollection<BIMember> Members { get; set; }
}

表格映射..

public class MapMember : EntityTypeConfiguration<BIMember>
{

    public MapMember()
        : base()
    {
        HasKey(b => b.ID).Property(b => b.ID).HasColumnName ("ID");
        Property(b => b.Name).HasColumnName ("NAME");
        Property(b => b.DateApplied).HasColumnName("DTM_APPLIED");
        Property(b => b.DateMembered).HasColumnName("DTM_MEMBERED");

        HasRequired(b=>b.MemberType)
            .WithMany(p=>p.Members)
            .Map(b => b.MapKey("TYPE_ID"));
        ToTable("MBR");

    }
}

public class MapMemberType : EntityTypeConfiguration<BIMemberType>
{

    public MapMemberType()
        : base()
    {
        HasKey(p => p.ID).Property(p => p.ID).HasColumnName("ID");
        Property(p => p.ID).HasColumnName("ID");
        Property(p => p.Description).HasColumnName("DESCRIPTION");

        ToTable("MBR_TYPE");

    }
}

和我的dbcontext ..

public class ManagerContext : DbContext
{
    public DbSet<BIMember> Members  { get; set; }
    public DbSet<BIMemberType> MemberTypes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new MapMemberType());
        modelBuilder.Configurations.Add(new MapMember());
        base.OnModelCreating(modelBuilder);
    }
}

Im谈论的属性是Member实体的BIMemberType属性。

该属性不会出现在我的视图中,但如果我将手动添加该属性,该属性将起作用。

此外,该属性应该在我的创建或编辑视图中显示为下拉列表。

提前致谢。

1 个答案:

答案 0 :(得分:0)

对于可能遇到此事的人......

以下是我的工作方式。

更改此代码
HasRequired(b=>b.MemberType)
 .WithRequiredDependent()
 .Map(b =>  b.MapKey("TYPE_ID"));

到这个

Property(b => b.MemberTypeID).HasColumnName("TYPE_ID");
        HasRequired(a => a.MemberType)
            .WithMany()
            .HasForeignKey(u => u.MemberTypeID);
MapMember类中的

并在会员类

添加了一个属性
public string MemberTypeID { get; set; }

由于