Search code examples
c#entity-frameworkdata-annotationspartial-classes

The type X conflicts with imported type X


In Entity Framework (Database first) I am trying to add some Data annotations to created classes.

In general: I have class X created:

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

    public partial class X
    {
        public string SomeProperty {get; set;}
        ...
    }
}

I want to property SomeProperty to be ignored when serializing to JSON thus in App_Code/Metadata I create class X.cs and I add some MetaData:

namespace Info
{

    public class XMetaData
    {
        [JsonIgnore]
        public string SomeProperty{get; set;}
    }

     [MetadataType(typeof(XMetaData))]
     public partial class X
     {

     }

}

Above I've manually changed namespace from Info.App_Code.Metadata to Info to have partial classes matched.

However in all places where I use X class i have warning

The type Info.X in '.../Info/App_Code/Metadata/X.cs ' conflicts with the imported type Info.X in '.../Info/X.cs'. Using the type defined in '.../Info/App_Code/Metadata/X.cs '

I expected that both partial classes would be merged however all occurrences are referring to that empty one X class.

Does anybody know what I am missing?


Solution

  • Depending on what type of application you are developing, it could be wrong to put code in the App_Code folder, as it has certain effects on the contents. See this other question.

    Try moving the source files out of App_Code and make sure their "Build action" is "Compiled" in the properties window.