Entity Framework Is Incomplete

c# entity-framework

Question

As I am studying the Entity Framework, I choose to build a simple console application with two tables that makes use of the framework.

Two tables have been made by me: http://wstaw.org/w/LrL/

I have classes that are appropriate for the database.

Here's the code:

The class of contacts.

public class Contact
{
     public int ContactID { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public ICollection<Address> Addresses { get; set; }
}

the Address class, too.

public class Address
{
     public int AddressID { get; set; }
     public string Street { get; set; }
     public string City { get; set; }
}

VS displays the following errors.

Error 3 Missing partial modifier on declaration of type 'MyEntityPOCO.Contact'; another partial declaration of this type exists Contact.c 8 19 MyEntityPOCO

Error 4 Missing partial modifier on declaration of type 'MyEntityPOCO.Address'; another partial declaration of this type exists Address.cs 8 19 MyEntityPOCO

Error 1 Running transformation: Please overwrite the replacement token '$edmxInputFile$' with the actual name of the .edmx file you would like to generate from. MyEntityPOCO\Model1.tt 1 1

How can I fix these mistakes?

1
2
11/11/2011 6:00:45 PM

Popular Answer

To define a class in many code files, use the partial keyword. Here's an explanation: http://msdn.microsoft.com/en-us/library/wa80x488.aspx

What has occurred in your instance is that you have two classes with the same name in the same namespace, one of which is decorated with the partial keyword (the one produced in the yourmodel.edmx.cs file by EF).

You would often get a class name conflict exception if there were two classes with the same name in the same namespace (without the partial keyword). The C# compiler produces the error you are seeing in this example because one of the two classes has a partial marking.

Search your code for the other class, figure out how it got there, and then rename it, relocate it to a new namespace, or, if it's the same class you intended, mark it with partial.

4
11/11/2011 5:40:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow