Type X conflicts with imported Type X

In the Entity Framework (database first) I am trying to add some data annotations to the generated classes.

In general: I have class X:

namespace Info { using System; using System.Collections.Generic; public partial class X { public string SomeProperty {get; set;} ... } } 

I want the SomeProperty property to SomeProperty ignored when serialized in JSON, so in App_Code/Metadata I create an X.cs class and add some metadata:

 namespace Info { public class XMetaData { [JsonIgnore] public string SomeProperty{get; set;} } [MetadataType(typeof(XMetaData))] public partial class X { } } 

Above, I manually changed the namespace from Info.App_Code.Metadata to Info to combine partial classes.

However, in all places where I use class X, I have a 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 both partial classes to be merged, however all occurrences relate to this empty class X.

Does anyone know what I am missing?

+4
source share
4 answers

Depending on what type of application you are developing, it would be wrong to put the code in the App_Code folder, since it has a certain effect on the content. See this other question .

Try moving the source files from App_Code and make sure that their "build action" is "compiled" in the properties window.

+5
source

Several definitions of partial classes that belong to the same class must exist within the same assembly. In the above example, during compilation, metadata should be baked into the class, and after compilation, the class is an integer, consisting of all parts. Partial classes are a way to split the definition of the same class into multiple files.

See here for a comprehensive explanation, but pay attention to the following:

All definitions of a partial type that must be part of the same type must be defined in the same assembly and in the same module (.exe or .dll file). Partial definitions cannot span multiple modules.

This link here explains that

In general, ASP.NET creates an assembly for each application directory (for example, App_Code) and one for the main directory.

In your case, although partial classes are in the same project and in the same namespace, they are not compiled into one assembly.

+4
source

delete the project bin folder, clean and rebuild the project

0
source

I had the same problem. Able to fix this in two ways. 1. Rename the App_Code folder 2. Move the .cs file to the project root directory.

Both methods work.

0
source

Source: https://habr.com/ru/post/1489719/


All Articles