I have created an asp.net core project, And also i have created one ClassLibrary and i have joined them to one solution. I write my way step by step :
1-dotnet new sln
2-dotnet new mvc -o GameShop
3-dotnet new classlib -o GameShop.Data
Then i created a class in GameShop.Data/Domain/User, I named it "User.cs"
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace GameShop.Data.Domain.User
{
public class User
{
[Key]
public int UserId { get; set; }
[Display(Name = "نام کاربری")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
[MaxLength(200, ErrorMessage = "{0} نمی تواند بیشتر از {1} باشد")]
public string UserName { get; set; }
[Display(Name = "ایمیل")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
[MaxLength(400, ErrorMessage = "{0} نمی تواند بیشتر از {1} باشد")]
[EmailAddress(ErrorMessage = "ایمیل وارد شده معتبر نمی باشد")]
public string Email { get; set; }
[Display(Name = "کلمه عبور")]
[Required(ErrorMessage = "لطفا {0} را وارد کنید")]
[MaxLength(200, ErrorMessage = "{0} نمی تواند بیشتر از {1} باشد")]
public string Password { get; set; }
[Display(Name = "تاریخ ثبت نام")]
public DateTime RegisterDate { get; set; }
[Display(Name = "کد فعال سازی")]
public string ActiveCode { get; set; }
}
}
This is my startup :
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<GameContext>(option =>
{
option.UseSqlServer(Configuration.GetConnectionString("GameShop"));
});
}
This my ConnectionString :
{
"ConnectionStrings": {
"GameShop": "Data Source=.;Initial Catalog=GameShop-DB;Integrated Security=True"
}
And this is my Context that its address is :(GameShop.Data/Context/GameContext)
using System;
using Microsoft.EntityFrameworkCore;
using GameShop.Data.Domain.User;
namespace GameShop.Data.Context
{
public class GameContext:DbContext
{
public GameContext(DbContextOptions<GameContext> option):base(option)
{
}
public DbSet<User> Users { get; set; }
}
}
Thats my codes. But whn i want to "add migrations", i have an error that says : Unable to create an object of type 'GameContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
These are the packages that i have in both GameShop.csproj and Gameshop.csproj :
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
</ItemGroup>
(I tried this without any ClassLibrary(just one "asp.net core mvc project ") and i didn`t have any error)
So can anyone help me what to do ???