我有这个示例代码:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Models;
namespace MySampleNamespace
{
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new UserMap(modelBuilder.Entity<User>());
}
public class UserMap
{
public UserMap(EntityTypeBuilder<User> entityBuilder)
{
entityBuilder.ToTable("User");
entityBuilder.Property(s => s.Username).HasMaxLength(15).IsRequired();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在MS网站上测试了一些例子,但我找不到ToTable方法.在这个例子中,我检查了什么是Usings,唯一使用的例子是Microsoft.EntityFrameworkCore,除了他正在使用的模型的类项目.这改变了吗?我现在该怎么办?
我正在尝试使用Include和获取一些值,ThenInclude但是EF Core生成的表名无效的语句。
这是我的DataContext:
public class BaseContext : BaseDbContext
{
protected BaseWebsiteContext(DbContextOptions dbContextOptions) : base(dbContextOptions)
{
}
public DbSet<Image> Images { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<ImageTags> ImageTags { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的三个数据模型:
public class Image : Entity.Entity
{
public string Title { get; set; }
public string Filepath { get; set; }
public DateTime CreateDate { get; set; }
public ICollection<ImageTags> ImageTags { get; set; } = new List<ImageTags>(); …Run Code Online (Sandbox Code Playgroud)