Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"

C#
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);
    }
}


Here In Page Load Em Trying To put Some Data..
C#
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);
                
            }
      


        }
Posted
Updated 3-Mar-15 6:42am
v2

1 solution

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; }
    }

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; }
 
    }


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; }
 
    }
 
Share this answer
 
Comments
nagendrathecoder 3-Mar-15 22:51pm    
Hi Dave,
I am also working on code first approach and have similar structure as OP mentioned but i did not received such error. Below is an example of how i am defining my data:

[Table("State")]
public partial class State
{
public int StateId { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }

[Required]
[StringLength(50)]
public string ShortName { get; set; }

public virtual ICollection<city> Cities { get; set; }
}

[Table("City")]
public partial class City
{
[Key]
public int CityId { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }

[Required]
[StringLength(50)]
public string ShortName { get; set; }

public int StateId { get; set; }

public virtual State State { get; set; }
}

Now suppose i removed int StateId from City class, EF will still add foreign key column of StateId in city table.
But while inserting into city, how i will be able to insert value for stateId?
Do i have to populate the Virtual State property? If yes then will it be a bit time consuming?
Dave Kreskowiak 3-Mar-15 23:48pm    
First, don't hijack someone elses thread.

If you go look at the database tables in the server you'll see that EF does this for you already. You don't have to put the ID field for a foreign key yourself.

You don't have to put anything in the virtual property unless there is an object to relate to it. The foreign key (State) in your model appears to be optional.

If you have a State to relate to this, all you have to do is assign the State object to the instance of this City. Or you can add your City object to the State Cities collection and EF will take care of the relation and assigning IDs for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900