我有以下模型对象
public class UserEntry
{
public int UserEntryID { get; set; }
public string UserID { get; set; }
public string TeamName { get; set; }
public int Total { get; set; }
public virtual ICollection<EntryPlayer> EntryPlayers { get; set; }
}
public class EntryPlayer
{
public int EntryPlayerID { get; set; }
public bool Captain { get; set; }
public virtual int UserEntryID { get; set; }
public virtual Player Player { get; set; }
}
public class Player
{
public int PlayerID { get; set; }
public string FirstName { get; set; }
public string MiddleInitial { get; set; }
public string LastName { get; set; }
public int Group { get; set; }
public string Team { get; set; }
public int Score { get; set; }
}
我的数据库架构如下所示: -
当我尝试使用此代码加载UserEntry时: -
UserEntry userEntry = this.db.UserEntries
.Where(u => u.UserID == user.Id)
.Include("EntryPlayers")
.FirstOrDefault();
我收到错误:
Invalid column name 'Player_PlayerID'
如果我更改Player
对象的UserEntry
属性:
public virtual Player Player { get; set; }
为:
public virtual int PlayerID { get; set; }
然后我的UserEntry
对象加载正常,但显然只有PlayerID
而不是整个Player
对象。
我需要更改哪些内容才能在Player
中加载UserEntry
对象?
我也有这个DatabaseInitializer类
namespace ACS.Models {
public class ACSDatabaseInitializer : CreateDatabaseIfNotExists<ACSContext>
{
protected override void Seed(ACSContext context)
{
base.Seed(context);
var players = new List<Player>();
players.Add(new Player
{
PlayerID = 1,
FirstName = "Dave",
MiddleInitial = "",
LastName = "Smith",
Group = 1,
Team = "Team1",
Score = 0
});
players.ForEach(p => context.Players.Add(p));
context.SaveChanges();
}
}
}
和这个Context类
namespace ACS.Models
{
public class ACSContext : DbContext
{
public ACSContext()
: base("name=ACS")
{
Database.SetInitializer<ACSContext>(null);
}
public DbSet<Player> Players { get; set; }
public DbSet<UserEntry> UserEntries { get; set; }
public DbSet<EntryPlayer> EntryPlayers { get; set; }
}
}
答案 0 :(得分:0)
我需要在UserEntry对象上同时使用PlayerID属性和Player属性才能使其工作