dbContext in an associated console app - Type exists in both client & server

hi guys,

this is not strictly Radzen, but i'm hoping you might have seen this before,

i'm writing a console app in the solution which is connecting to the Radzen database context.

i have it all working and it's able to connect and linq query the DB just fine

however when i go to create a new DB class i.e. "stuff" i'm writing

var stuff = new RadzenSolution.Models.RadzenDB.Stuff()

this is throwing an error

Severity Code Description Project File Line Suppression State
Error CS0433 The type 'Stuff' exists in both 'RadzenSolution.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'RadzenSolution.Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' Stuff C:\Projects\RadzenSolution\Stuff\Stuff.cs 212 Active

i can't seem to figure out how to stop this as i think it comes from including the server reference in the console app, but the server app then incldes the client. hence the error.....

According to the error you have same class defined in two projects.

yes, it's because the client is referenced in the server, and i'm referencing the server in my console app.

the question is, how do you break this reference?

i need to use the database model and dbContext from the server, i don't need anything from the client

I’m afraid that I will unable to provide any other information. You can use StackOverflow for such questions.

thanks Enchev, yeah i can't quite get this pattern to work.
should be fairly simple....
a console app which uses the server DBContext which can change the DB connection string based on a paramater parsed into it.

it's fairly simple but this conflict is breaking the console app build....

it looks like the pattern to get the Models included in the client app is in the CSPROJ settings

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
  <PropertyGroup>
    <NoWarn>CS0168,CS1998,BL9993,CS0649,CS0436,0436,CS8073,CS4014,CS1717</NoWarn>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Append.Blazor.Clipboard" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="QRCoder" Version="1.4.2" />
    <PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
    <PackageReference Include="Radzen.Blazor" Version="4.14.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.0" />
    <Compile Include="../server/Models/**" />
  </ItemGroup>
</Project>

and the Server project has a reference to the client project....

what i have done in this console app is reference the server project

	<ItemGroup>
    <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.15.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>

	<ItemGroup>
	  <ProjectReference Include="..\server\TmrGovernance.Server.csproj" />
	</ItemGroup>

this does give me access to the server and dbContext, etc. but it's also the cause of this conflict....

i have tried to replicate the Client configuration reference wise but i then lost the ability to get to the Context.Database.stuff

i feel this is harder than it needs to be :smiley:

You can include desired files (models, dbContext, etc.) similar to the Client app:



Yep thanks @enchev i managed to break my pattern and use similar to the above
i ended up with this which solved all problems.

	<ItemGroup>
		<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.15.0" />
		<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
		<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">
		  <PrivateAssets>all</PrivateAssets>
		  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
		</PackageReference>
		<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
		<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
		<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
		<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
		<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
		<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
		<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.4" />
		<Compile Include="../server/Models/**" />
		<Compile Remove="..\server\Models\ApplicationUser.cs" />
		<Compile Include="../server/Data/TmrRadzenContext.cs" />
	</ItemGroup>

i was able to then establish the DB Context and access the models without conflict of project location.

thanks for the help as always