Asynchronous
Asynchronous

Reputation: 3977

Entity Framework does not contain definition for existing table object

I am relatively new to Entity FrameWork. I am using the AdventureWorks Database as a learning resource. I am also following this tutorial: https://msdn.microsoft.com/en-us/data/jj206878.aspx

My problem is I am unable to insert into multiple tables, specifically two tables: This is not because an insert is failing but rather the Object for the Person entity is not available to perform an Add.

The following code adds a new record to the Person.BusinessEntity table.

class Program
{
    static void Main(string[] args)
    {
        int errorLength = 0;

        using(var dataAccessContext = new DataAccessContext())
        {
            try
            {
                Console.Write("Processing data, please wait...");

                Guid guid = Guid.NewGuid();

                BusinessEntity businessEntity = new BusinessEntity
                {
                    rowguid = guid,
                    ModifiedDate = DateTime.Now
                };

                dataAccessContext.BusinessEntities.Add(businessEntity);
                dataAccessContext.SaveChanges();

                Console.Clear();

            }
            catch(Exception ex)
            {
                errorLength = ex.Message.Trim().Length;

                Console.WriteLine("Data Process Failed:");
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if(errorLength <= 0)
                {
                    Console.WriteLine("Data successfully processed.");
                    Console.Write("Press any key to exit: ");
                }
            }

            Console.ReadKey();
        }
    }
}

However, I would also like to create a new person by inserting a record into the Person.Person table. However, the DataAccessContext() does not expose the Person object for adding a Person record:

The following does not work:

                Person person = new Person
                {
                    BusinessEntityID = Convert.ToInt32(businessID),
                    PersonType = personType,
                    NameStyle = Convert.ToBoolean(nameStyle),
                    FirstName = firstName,
                    MiddleName = middleName,
                    LastName = lastName,
                    EmailPromotion = Convert.ToInt32(emailPromotion),
                    rowguid = guid,
                    ModifiedDate = DateTime.Now
                };

                dataAccessContext.Person.Add(person);
                dataAccessContext.SaveChanges();

I get the following error message:

DataAccessServices.DataAccessContext' does not contain a definition for 'Person' and no extension method 'Person' accepting a first argument of type 'DataAccessServices.DataAccessContext' could be found (are you missing a using directive or an assembly reference?

Upvotes: 0

Views: 7037

Answers (1)

Josh Jay
Josh Jay

Reputation: 1250

It sounds like you need to add the Person definition to the DataAccessContext class.

https://msdn.microsoft.com/en-us/data/jj592675.aspx

Or maybe the object already exists as DataAccessContext.People or DataAccessContext.Persons. Usually the collection on the data access context is the plural form of the individual model.

Upvotes: 1

Related Questions