Suffii
Suffii

Reputation: 5784

Having issue on adding data to Entity Framework table

Can you please let me know why I am not able to add data to Region table here?

protected void Button4_Click(object sender, EventArgs e)
{
    Region newRegion = new Region()
    {
        RegionID = 5000,
        RegionDescription = TextBox1.Text

    };

    db.Regions.Add(newRegion);
    db.SaveChanges();
}

I am getting this error:

enter image description here

at:

db.Regions.Add(newRegion);

I have a Region class as:

namespace EFDataStore
{
    using System;
    using System.Collections.Generic;

    public partial class Region
    {
        public int RegionID { get; set; }
        public string RegionDescription { get; set; }
    }
}

Upvotes: 0

Views: 32

Answers (1)

Win
Win

Reputation: 62311

It seems that you have namespace conflict. You will need full namespace for Region class.

protected void Button4_Click(object sender, EventArgs e)
{
    var newRegion = new EFDataStore.Region() <==== Need full namespace.
    {
        RegionID = 5000,
        RegionDescription = TextBox1.Text

    };

    db.Regions.Add(newRegion);
    db.SaveChanges();

}

Upvotes: 2

Related Questions