DataContext 总是 returns null 作为外键
DataContext always returns null as foreign key
我们目前正在开发一个 ASP.NET 页面。
我们正在使用 EntityFramework (EF) 并创建了一个 BaseController 来处理我们的 DataContext。
简化后我们有 2 个模型:
用户模型(来自自动生成的代码):
public class User : IdentityUser
{
public virtual ICollection<Location> Locations { get; set; }
public virtual ICollection<Calendar> Calendars { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
还有我们自己的日历模型:
public class Calendar
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[DataType(DataType.Text)]
public string GoogleId { get; set; }
[Required]
[DataType(DataType.Text)]
public string Title { get; set; }
[DataType(DataType.Text)]
public string Description { get; set; }
public ICollection<Event> Events { get; set; }
public User User { get; set; }
}
所以我们在这些模型之间建立了联系:
一个用户可以有多个日历。
但是只有一个日历可以来自一个用户。
以下代码是我们的DataContext:
public class DatabaseContext : IdentityDbContext<User>
{
public DatabaseContext()
{
// ReSharper disable once UnusedVariable
var instance = SqlProviderServices.Instance;
}
public static DatabaseContext Create()
{
return new DatabaseContext();
}
public DbSet<Location> Locations { get; set; }
public DbSet<Calendar> Calendars { get; set; }
public DbSet<Event> Events { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().HasMany(c => c.Calendars);
}
}
现在的问题是,当我使用 DataContext 时,我从数据库中获取了所有内容。但是
DataContext db = new DataContext()
db.Calendar ...
在日历中,用户始终为空!
当我在以下代码中使用 ASP.NET MVC 将日历添加到数据库时:
public ActionResult Create([Bind(Include = "Title,Description")] Models.Calendar calendar)
{
if (ModelState.IsValid)
{
calendar.User = CurrentUser;
calendar.Events = new List<Event>();
Db.Calendars.Add(calendar);
Db.SaveChanges();
return RedirectToAction("Index");
}
return View(calendar);
}
我调试了这个方法,在 IF 的第一行它确实将正确的用户设置到日历中。
谁能告诉我,为什么 Calendar.User 总是空的?
第一件重要的事情:创建日历时,CurrentUser
对象应该附加到上下文。
我不确定您是否打开或关闭了上下文的 LazyLoading,但您始终可以尝试这样查询日历:
(using System.Data.Entity;)
Db.Calendars.Include(calendar => calendar.User).Where(...);
或
Db.Calendars.Include("User").Where(...);
这应该会强制 EF 从数据库中加载链接的实体。试试这个,看看它是否适合你。
我们目前正在开发一个 ASP.NET 页面。 我们正在使用 EntityFramework (EF) 并创建了一个 BaseController 来处理我们的 DataContext。
简化后我们有 2 个模型:
用户模型(来自自动生成的代码):
public class User : IdentityUser
{
public virtual ICollection<Location> Locations { get; set; }
public virtual ICollection<Calendar> Calendars { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
还有我们自己的日历模型:
public class Calendar
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[DataType(DataType.Text)]
public string GoogleId { get; set; }
[Required]
[DataType(DataType.Text)]
public string Title { get; set; }
[DataType(DataType.Text)]
public string Description { get; set; }
public ICollection<Event> Events { get; set; }
public User User { get; set; }
}
所以我们在这些模型之间建立了联系: 一个用户可以有多个日历。 但是只有一个日历可以来自一个用户。
以下代码是我们的DataContext:
public class DatabaseContext : IdentityDbContext<User>
{
public DatabaseContext()
{
// ReSharper disable once UnusedVariable
var instance = SqlProviderServices.Instance;
}
public static DatabaseContext Create()
{
return new DatabaseContext();
}
public DbSet<Location> Locations { get; set; }
public DbSet<Calendar> Calendars { get; set; }
public DbSet<Event> Events { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().HasMany(c => c.Calendars);
}
}
现在的问题是,当我使用 DataContext 时,我从数据库中获取了所有内容。但是
DataContext db = new DataContext()
db.Calendar ...
在日历中,用户始终为空!
当我在以下代码中使用 ASP.NET MVC 将日历添加到数据库时:
public ActionResult Create([Bind(Include = "Title,Description")] Models.Calendar calendar)
{
if (ModelState.IsValid)
{
calendar.User = CurrentUser;
calendar.Events = new List<Event>();
Db.Calendars.Add(calendar);
Db.SaveChanges();
return RedirectToAction("Index");
}
return View(calendar);
}
我调试了这个方法,在 IF 的第一行它确实将正确的用户设置到日历中。 谁能告诉我,为什么 Calendar.User 总是空的?
第一件重要的事情:创建日历时,CurrentUser
对象应该附加到上下文。
我不确定您是否打开或关闭了上下文的 LazyLoading,但您始终可以尝试这样查询日历:
(using System.Data.Entity;)
Db.Calendars.Include(calendar => calendar.User).Where(...);
或
Db.Calendars.Include("User").Where(...);
这应该会强制 EF 从数据库中加载链接的实体。试试这个,看看它是否适合你。