Search code examples
c#asp.netvisual-studionugetnuget-package

CS0433 The type 'type' exists in 'ProjectA' and 'ProjectB'


I have a solution made with .NET Framework 4.7 (c#) in which we have 2 projects that contain the same partial class within the same namespace, those projects are packaged on a nupkg.

From now on we will follow the following logic:

  • Project A: has a part of the partial class and reference to Project B
  • Project B: has another part of the partial class (code generated automatically)
  • Project C: this project has the nupkg referenced but if we try to call any property of the mentioned class when compiling, it gives the following error: CS0433 The type 'Config' exists in 'ProjectA, Version=1.2023.2.21, Culture=neutral, PublicKeyToken=null' and in 'ProjectB, Version=1.2023.2.21, Culture=neutral, PublicKeyToken=null'

I've already tried:

  • Cleaning and rebuilding.
  • Deleting and adding again the references.
  • Removing redundant and useless references.
  • Uninstalling the NuGet from Project C and installing it again.

Here are the conflicting code snippets in each project:

Project A: partial class Config

namespace WhatIsWrong
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Config
    {
    }
}

Project B: partial class Config

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

    public partial class Config
    {
        public string parameter { get; set; }
        public string value { get; set; }
    }
}

Project C

public DbSet<WhatIsWrong.Config> Config { get; set; }

Update:

I already thought that partial classes had to be on the same Project like Jon and Damien pointed on the comments, but this solution was created by someone else and was supposed to be working, I guess his idea was to have just one model for both projects (what is not wrong) but he also tried to play with partials in both projects what ended with that error.


Solution

  • Project A: has a part of the partial class and reference to Project B
    Project B: has another part of the partial class (code generated automatically)

    That's not how partial classes work. What you've got there is two classes in two projects.

    Every part of a partial class needs to be in the same project, because they're compiled together to be one type.

    Partial classes allow a single type's code to be distributed amongst multiple files - but not multiple projects.