这个EF Code First Model有什么错误? [英] What Wrong With This EF Code First Model..?

查看:76
本文介绍了这个EF Code First Model有什么错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是接收的例外情况。

属性'MeterID'不能配置为导航属性。该属性必须是有效的实体类型,并且属性应该具有非抽象的getter对于集合属性,类型必须实现ICollection< t>其中T是有效的实体类型



This Is The Exception Em Recieving.
"The property 'MeterID' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<t> where T is a valid entity type"

public class Employee
{
    public int EmployeeID { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public float Salary { get; set; }
    public int Age { get; set; }
    public String MobileNo { get; set; }
    public String Designation { get; set; }

    [ForeignKey("QuaterID")]
    public int QuaterID { get; set; }
    public DomesticQuater Quater { get; set; }


}

public class DomesticQuater
{
    public int ID { get; set; }
    public Int64 QuaterNo { get; set; }
    public String QuaterType { get; set; }
    public int Block { get; set; }
    public String Area { get; set; }
    public String Street { get; set; }
    public String AddtionalAddress { get; set; }

    [ForeignKey("MeterID")]
    public int MeterID { get; set; }
    public virtual ElectricMeter Meter { get; set; }

}

public class ElectricMeter
{
    public int MeterID { get; set; }
    public int MeterNo   { get; set; }
    public DateTime InstalledDate { get; set; }
    [Column("MeterType")]
    public bool IsDomestic { get; set; }

}

[Table("CommercialConsumers")]
public class CommercialShop
{
    public int ID { get; set; }
    public Int64 OwnerNIC { get; set; }
    public string OwnerName { get; set; }
    public String Block { get; set; }
    public String Area { get; set; }
    public String Street { get; set; }
    public String AddtionalAddress { get; set; }

    [ForeignKey("MeterID")]
    public int MeterID { get; set; }
    public virtual ElectricMeter Meter { get; set; }

}


public class DemoBillingContext : DbContext
{


    public virtual DbSet<Employee> Employees { get; set; }
    public virtual DbSet<ElectricMeter> Meters { get; set; }
    public virtual DbSet<DomesticQuater> Quaters { get; set; }
    public virtual DbSet< CommercialShop>CommercialConsumers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
                base.OnModelCreating(modelBuilder);
    }
}





这里在页面加载Em试图放一些数据..



Here In Page Load Em Trying To put Some Data..

protected void Page_Load(object sender, EventArgs e)
        {


            DemoBillingContext context = new DemoBillingContext();

            //context.Meters.Add()
            ElectricMeter m = new ElectricMeter()
                                {
                                    MeterNo = 56589255,
                                    IsDomestic = true,
                                    InstalledDate = DateTime.Now
                                };
            DomesticQuater q = new DomesticQuater()
                {
                    QuaterNo = 563,
                    Area = "Loco",
                    Block = 3,
                    QuaterType = "OfficerQuater",
                    Street = "45",
                    MeterID = 1,AddtionalAddress="LOCO FACTROY 1 STREET 1 HOUSE 23/43",

                };
            Employee emp = new Employee
            {


                FirstName = "ARIF",
                LastName = "HUSSAIN",
                MobileNo = "03475060769",
                Designation = "Manager",
                Salary = 50000,
                QuaterID = 1,

            };

            context.Employees.Add(emp);

            foreach (var item in context.Employees)
            {

                Domestic.Text += String.Format("{0}---{1}---{2}---{3}--{4}",item.FirstName+" "+item.LastName,
                    item.Salary,
                    item.Quater.QuaterNo,
                    item.Quater.Area,
                    item.Quater.Meter.MeterNo,
                    item.Quater.Meter.IsDomestic);
                
            }
      


        }

推荐答案

您的课程看起来应如下所示:

It appears that your class should look like this:
public class DomesticQuater
    {
        public int ID { get; set; }
        public Int64 QuaterNo { get; set; }
        public String QuaterType { get; set; }
        public int Block { get; set; }
        public String Area { get; set; }
        public String Street { get; set; }
        public String AddtionalAddress { get; set; }
 
        public virtual ElectricMeter Meter { get; set; }
    }



根本不需要int MeterId行。 EF会把它放在那里。您在 CommercialShop Employee 类中遇到同样的问题。



你的 ElectricMeter 类也有问题。对于传统配置,ID列名称应该是附加了Id的类的名称:


You don't need the "int MeterId" line at all. EF will put it in there for you. You have the same problem in your CommercialShop and Employee classes.

Your ElectricMeter class also has a problem. The ID column name should be the name of the class with "Id" appended to it for a conventional configuration:

public class ElectricMeter
    {
        public int ElectricMeterID { get; set; }
        public int MeterNo { get; set; }
        public DateTime InstalledDate { get; set; }
        [Column("MeterType")]
        public bool IsDomestic { get; set; }
 
    }





或者您可以明确地将其标记为表格的关键,但只需确保你的代码中没有 MeterId MeterNo 混淆。



OR you can explicitly tag it as the key to the table, but just make sure you don't get MeterId and MeterNo confused in your code.

public class ElectricMeter
    {
        [Key]
        public int MeterID { get; set; }
        public int MeterNo   { get; set; }
        public DateTime InstalledDate { get; set; }
        [Column("MeterType")]
        public bool IsDomestic { get; set; }
 
    }


这篇关于这个EF Code First Model有什么错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆