gpt4 book ai didi

C# Entity Framework 6 MySQL - 插入 ApplicationUser_Id 时引用的未知列

转载 作者:行者123 更新时间:2023-11-29 02:46:25 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 6 和 MySQL。这是我的两个模型:

public class AppSession
{
[Key]
public int Id { get; set; }

[MaxLength(36)]
public string SesId { get; set; }

[ForeignKey("User")]
public string UserId { get; set; }

public DateTime CreatedOn { get; set; }

public virtual ApplicationUser User { get; set; }

public virtual ICollection<AppSessionBreakdown> Breakdown { get; set; }
}

public class AppSessionBreakdown
{
[Key]
public int Id { get; set; }

[ForeignKey("AppSession")]
public int ASId { get; set; }

public string FileName { get; set; }

public virtual AppSession AppSession { get; set; }
}

我用于插入的 C# 代码是:

var breakdown = new AppSessionBreakdown {   ASId = snapSession.Id, FileName = fileName }; 
repositoryAppSessionsBreakdown.Insert(breakdown);
repositoryAppSessionsBreakdown.Save();

出于某些原因,当我检查生成的 SQL 时,EF6 添加了一个未在任何地方指定的额外列 (ApplicationUser_Id),插入语句如下所示:

INSERT INTO `AppSessionBreakdowns`(`ASId`, FileName`, `ApplicationUser_Id`) VALUES (1, @gp1, NULL);

如果我将 ApplicationUser 列添加到 AppSessionBreakdown 模型,它确实有效,但我可以做些什么来防止这种情况发生,因为我不想添加无用的列使它正常工作。

应用用户模型:

public class ApplicationUser : IdentityUser
{
[Display(Name = "First Name")]
public string FirstName { get; set; }

[Display(Name = "Surname")]
public string LastName { get; set; }

public bool Activated { get; set; }

[DisplayFormat(DataFormatString = "({0:dd MMM yyyy})")]
[Display(Name = "Activated On")]
public DateTime? ActivatedOn { get; set; }

[DisplayFormat(DataFormatString = "({0:dd MMM yyyy})")]
[Display(Name = "Last Logged In")]
public DateTime? LastLoggedIn { get; set; }

public int ContractorId { get; set; }

public bool TestAccount { get; set; }

public virtual ICollection<TapInSite> TapInSite { get; set; }

public virtual ICollection<TapInLog> TapInLog { get; set; }

public virtual ICollection<AppSession> SnapAppSessions { get; set; }

public virtual ICollection<AppSessionBreakdown> SnapAppSessionsBreakdown { get; set; }

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
}
}

最佳答案

ApplicationUserAppSessionBreakdown 之间存在一对多关系,例如:

enter image description here

结果将是:

enter image description here

有关更多信息,请阅读 Entity Framework Relationships and Navigation Properties .

关于C# Entity Framework 6 MySQL - 插入 ApplicationUser_Id 时引用的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41544608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com