C# DbContext返回一些空字段

C# DbContext返回一些空字段,c#,database,entity-framework,C#,Database,Entity Framework,Q:当我尝试从\u Db返回数据时,我在列表属性中得到null对象,我想知道为什么其他属性返回正确,而列表没有 public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext> { protected override void Seed(ApplicationDbContext context) { context.Players.Add(ne

Q:当我尝试从
\u Db
返回数据时,我在
列表
属性中得到null对象,我想知道为什么其他属性返回正确,而
列表
没有

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
问题解释:我有一个
ApplicationDbContext
公共IDbSet播放器{get;set;}
属性。使用以下
ApplicationBinInitializer
类:

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
我对数据的请求如下所示:

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
public class Player
{
    [Key]
    public int Id { get; set; }
    public string PlayerName { get; set; }
    public List<string> PlayerSkills { get; set; }
 }
public IEnumerable<Player> Get()
{
    return _Db.Players;
}

SQL中有一种类型的字段可以存储值集合。若你们看一下EF生成的表格,你们将不会看到
PlayerSkills
归档在那个里。创建脚本将如下所示

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
CREATE TABLE [dbo].[Players] (
    [Id] [int] NOT NULL IDENTITY,
    [PlayerName] [nvarchar](max),
    CONSTRAINT [PK_dbo.Players] PRIMARY KEY ([Id])
)
这就是为什么在
PlayerSkills
字段中没有任何数据。若要在SQL数据库中存储一对多关系,则需要第二个表来存储玩家技能以及这些技能相关的玩家id。如果您想避免技能重复,甚至可以使用两个表—一个用于技能,另一个用于将玩家映射到技能

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}

没有连接表(如果您不希望外键具有显式属性,那么您的选项是fluent映射而不是属性映射):

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
玩家

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
public class Player
{
    public int Id { get; set; }
    public string PlayerName { get; set; }
    public virtual List<PlayerSkill> PlayerSkills { get; set; }
}
公共类播放器
{
公共int Id{get;set;}
公共字符串播放器名称{get;set;}
公共虚拟列表玩家技能{get;set;}
}

带连接表

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Player>()
        .HasMany(p => p.PlayerSkills)
        .WithMany()
        .Map(j => j.MapLeftKey("PlayerId")
                   .MapRightKey("PlayerSkillId")
                   .ToTable("PlayerToSkill"));

    base.OnModelCreating(modelBuilder);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(p=>p.PlayerSkills)
.有很多
.Map(j=>j.MapLeftKey(“PlayerId”)
.MapRightKey(“PlayerSkillId”)
.ToTable(“PlayerToSkill”);
基于模型创建(modelBuilder);
}

SQL中有一种类型的字段可以存储值集合。若你们看一下EF生成的表格,你们将不会看到
PlayerSkills
归档在那个里。创建脚本将如下所示

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
CREATE TABLE [dbo].[Players] (
    [Id] [int] NOT NULL IDENTITY,
    [PlayerName] [nvarchar](max),
    CONSTRAINT [PK_dbo.Players] PRIMARY KEY ([Id])
)
这就是为什么在
PlayerSkills
字段中没有任何数据。若要在SQL数据库中存储一对多关系,则需要第二个表来存储玩家技能以及这些技能相关的玩家id。如果您想避免技能重复,甚至可以使用两个表—一个用于技能,另一个用于将玩家映射到技能

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}

没有连接表(如果您不希望外键具有显式属性,那么您的选项是fluent映射而不是属性映射):

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
玩家

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
public class Player
{
    public int Id { get; set; }
    public string PlayerName { get; set; }
    public virtual List<PlayerSkill> PlayerSkills { get; set; }
}
公共类播放器
{
公共int Id{get;set;}
公共字符串播放器名称{get;set;}
公共虚拟列表玩家技能{get;set;}
}

带连接表

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        context.Players.Add(new Player { PlayerName = "john", PlayerSkills = new List<string> { "a", "b" } });
        context.Players.Add(new Player { PlayerName = "Wuli", PlayerSkills = new List<string> { "c", "d" } });

        base.Seed(context);
    }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Player>()
        .HasMany(p => p.PlayerSkills)
        .WithMany()
        .Map(j => j.MapLeftKey("PlayerId")
                   .MapRightKey("PlayerSkillId")
                   .ToTable("PlayerToSkill"));

    base.OnModelCreating(modelBuilder);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(p=>p.PlayerSkills)
.有很多
.Map(j=>j.MapLeftKey(“PlayerId”)
.MapRightKey(“PlayerSkillId”)
.ToTable(“PlayerToSkill”);
基于模型创建(modelBuilder);
}

从SQL的角度来看,
PlayerSkills
需要存储字符串集合的字段类型是什么?好吧,就这样吧。。是代码优先、模型优先、数据库优先吗?如果首先是数据库——也许是。还有一点可能会影响事情。。它是实体核心吗?还是实体框架6?啊!!查看您的
公共列表玩家技能
我想知道您是否理解..@BagusTesa它是代码优先和实体框架6。是的,我可能错过了一些概念-第一次使用db。从SQL的角度来看,
PlayerSkills
必须存储字符串集合的字段类型是什么。。是代码优先、模型优先、数据库优先吗?如果首先是数据库——也许是。还有一点可能会影响事情。。它是实体核心吗?还是实体框架6?啊!!查看您的
公共列表玩家技能
我想知道您是否理解..@BagusTesa它是代码优先和实体框架6。是的,我可能错过了一些概念——第一次与db合作。