I have a SQL-Server database that I want to use in my c# application.
CREATE TABLE [dbo].[Marks] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[CategoryId] INT NOT NULL,
[Name] NVARCHAR(MAX) NOT NULL,
[Value] DECIMAL(18,3) NOT NULL
)
CREATE TABLE[dbo].[Categories] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR(MAX) NOT NULL,
[Average] decimal(18,3) NOT NULL
)
CREATE TABLE[dbo].[Subjects] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR(MAX) NOT NULL,
[Teacher] NVARCHAR(MAX) NOT NULL
[TestsId] INT NOT NULL,
[WrittenId] INT NOT NULL,
)
I converted the database tables to C# classes:
public partial class Marks
{
public int Id { get; set; }
public int SubjectId { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public decimal Value { get; set; }
public Categories Category { get; set; }
public Subjects Subject { get; set; }
}
public partial class Subjects
{
public int Id { get; set; }
public string Name { get; set; }
public string Teacher { get; set; }
public ICollection<Marks> Marks { get; set; }
}
public partial class Categories
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Marks> Marks { get; set; }
}
I want to get one subject and then for each category the corresponding marks. For every Subject there should be multiple Categories. How do I achieve this with Entity Framework Core 2.0?
Mark is your many to many table and you say every Subject there should be multiple Categories.
I wrote the code using in-memory provider. You can change to your table structure like this.
First off all your entity naming must be singular.
Your entities should be as follows;
public class Subject
{
public Subject()
{
Marks = new List<Mark>();
Categories = new List<Category>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Teacher { get; set; }
public ICollection<Mark> Marks { get; set; }
public ICollection<Category> Categories { get; set; }
}
public class Category
{
public Category()
{
Marks = new List<Mark>();
}
public int Id { get; set; }
public string Name { get; set; }
public int SubjectId { get; set; }
public Subject Subject { get; set; }
public ICollection<Mark> Marks { get; set; }
}
public class Mark
{
public int Id { get; set; }
public int SubjectId { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public decimal Value { get; set; }
public Category Category { get; set; }
public Subject Subject { get; set; }
}
Then your DataContex should be as follows;
public class AppDataContext : DbContext
{
public AppDataContext(DbContextOptions<AppDataContext> options)
: base(options)
{
}
public DbSet<Subject> Subjects { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Mark> Marks { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Category>().HasMany(c => c.Marks).WithOne(m => m.Category).HasForeignKey(m => m.CategoryId);
modelBuilder.Entity<Subject>().HasMany(s => s.Marks).WithOne(m => m.Subject).HasForeignKey(m => m.SubjectId);
modelBuilder.Entity<Subject>().HasMany(s => s.Categories).WithOne(c => c.Subject).HasForeignKey(c => c.SubjectId);
}
}
So sample code is as follows;
static void Main(string[] args)
{
var context = new AppDataContext(new DbContextOptionsBuilder<AppDataContext>().UseInMemoryDatabase("test")
.Options);
var category11 = new Category {Id = 11, Name = "Category-11"};
var subject22 = new Subject {Id = 22, Name = "Subject-22"};
subject22.Categories.Add(category11);
var mark1 = new Mark
{
Id = 1,
Name = "Mark-1",
CategoryId = 11,
SubjectId = 22,
Category = category11,
Subject = subject22
};
context.Categories.Add(category11);
context.Subjects.Add(subject22);
context.Marks.Add(mark1);
context.SaveChanges();
var markList = context.Marks.ToList();
foreach (var mark in markList)
{
Console.WriteLine(mark.Name);
Console.WriteLine(mark.Subject.Categories.FirstOrDefault().Name);
}
Console.ReadKey();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.