0

I am developing an asp.net mvc application, which has these enity classes:

public class Person
    {
        public int PersonID { get; set; }
        public string PersonPicAddress { get; set; }

        public virtual List<Person_Local> PersonLocal { get; set; }
}

public class Person_Local
{
    public int PersonID { get; set; }

    public int CultureID { get; set; }

    public string PersonName { get; set; }

    public string PersonFamily { get; set; }

    public string PersonAbout { get; set; }

    public virtual Culture Culture { get; set; }

    public virtual Person Person { get; set; }
}

public class Culture
{
    public int CultureID { get; set; }

    [Required()]
    public string CultureName { get; set; }

    [Required()]    
    public string CultureDisplay { get; set; }

    public virtual List<HomePage> HomePage { get; set; }

    public virtual List<Person_Local> PersonLocak { get; set; }
}

I defined an action with [Httppost] attribute, which accepts complex object from a view.

Here is the action :

[HttpPost]
public ActionResult CreatePerson([Bind(Prefix = "Person")]Person obj)
        {

      AppDbContext da = new AppDbContext();

      //Only getting first PersonLocal from list of PersonLocals
      obj.PersonLocal[0].Person = obj;

       da.Persons.Add(obj);

       da.SaveChanges();

       return Jsono(...);

}

But when it throws error as below :

Exception:Thrown: "Invalid column name 'Culture_CultureID'." (System.Data.SqlClient.SqlException)
A System.Data.SqlClient.SqlException was thrown: "Invalid column name 'Culture_CultureID'."

And the insert statement :

ADO.NET:Execute Reader "insert [dbo].[Person_Local]([PersonID], [PersonName], [PersonFamily], [PersonAbout], [Culture_CultureID])
values (@0, @1, @2, @3, null)
select [CultureID]
from [dbo].[Person_Local]
where @@ROWCOUNT > 0 and [CultureID] = scope_identity()"
The command text "insert [dbo].[Person_Local]([PersonID], [PersonName], [PersonFamily], [PersonAbout], [Culture_CultureID])
values (@0, @1, @2, @3, null)
select [CultureID]
from [dbo].[Person_Local]
where @@ROWCOUNT > 0 and [CultureID] = scope_identity()" was executed on connection "Data Source=bab-pc;Initial Catalog=MainDB;Integrated Security=True;Application Name=EntityFrameworkMUE", building a SqlDataReader.

Where is the problem?

Edited:

Included EntityConfigurations Code:

   public class CultureConfig : EntityTypeConfiguration<Culture>
    {
        public CultureConfig()
        {

            HasKey(x => x.CultureID);

            Property(x => x.CultureName);

            Property(x => x.CultureDisplay);

            ToTable("Culture");
        }
    }

    public class PersonConfig : EntityTypeConfiguration<Person>
    {
        public PersonConfig()
        {

            HasKey(x => x.PersonID);

            Property(x=>x.PersonPicAddress);

            ToTable("Person");
        }
    }

    public class Person_LocalConfig : EntityTypeConfiguration<Person_Local>
    {
        public Person_LocalConfig()
        {
            HasKey(x => x.PersonID);

            HasKey(x => x.CultureID);

            Property(x=>x.PersonName);

            Property(x => x.PersonFamily);

            Property(x => x.PersonAbout);

            ToTable("Person_Local");

          }

    }

2 Answers 2

0

Try to remove fields CultureID and PersonID from Person_Local class. Because you already has field Person and Culture

Sign up to request clarification or add additional context in comments.

3 Comments

Removing them breaks my view templates down.
Anyway it's wrong to define foreign keys and links to objects in one class. You just add fields with links to objects. If you use foreign keys in views you can use them too. Simply use Model.Person.PersonID and Model.Culture.CultureID.
Still getting the same error even by removing those int properties. Those key you have mentioned to remove, are primary keys of Person_Local.
0

It looks like your schema is out of sync with your model. Make sure you understand EF Code first schema update features, which are described in this blog. If you need more sophisticated schema migration, there are some other approaches in answers to this question.

1 Comment

There is not any mismatch between the schema in database and what I have defined in each EntityConfiguration of an entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.