Terravision
Terravision
CC#
Created by Terravision on 10/27/2023 in #help
❔ asp.net mvc database connection not working
someone help please am trying to connect database mysql with asp.net mvc app but am encountering this error, am using .net cli C:\Users\Faiza Abdella\Desktop\mvcdotnet\faizamvc\Data\dbcontexts.cs(29,34): error CS0246: The type or namespace name ' ApplicationDbContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\Faiza Abdella\Desktop\mvcdotnet\faizamvc\faizamvc.csproj]
faizamvc.csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
faizamvc.csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
dbcontext.cs file
using Microsoft.EntityFrameworkCore;

public class YourDbContext : DbContext
{
public YourDbContext(DbContextOptions<YourDbContext> options) : base(options)
{
}

// DbSet properties representing your database tables
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string? Name { get; set; }
// Other properties
}
public class Order
{
public int OrderId { get; set; }
public string? CustomerName { get; set; }
// Other properties
}

public class Startup
{
public void ConfigureServices(IServiceCollection services)
=> services.AddDbContext<ApplicationDbContext>();
}
dbcontext.cs file
using Microsoft.EntityFrameworkCore;

public class YourDbContext : DbContext
{
public YourDbContext(DbContextOptions<YourDbContext> options) : base(options)
{
}

// DbSet properties representing your database tables
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string? Name { get; set; }
// Other properties
}
public class Order
{
public int OrderId { get; set; }
public string? CustomerName { get; set; }
// Other properties
}

public class Startup
{
public void ConfigureServices(IServiceCollection services)
=> services.AddDbContext<ApplicationDbContext>();
}
3 replies