gpt4 book ai didi

asp.net - Entity Framework 可以生成从外键到父表中 GUID 的映射吗?

转载 作者:行者123 更新时间:2023-12-03 06:35:07 25 4
gpt4 key购买 nike

我的数据库有两个这样的表(删除了一些列以保持简单)

CREATE TABLE [dbo].[Question] (
[QuestionId] INT IDENTITY (1, 1) NOT NULL,
[ProblemId] INT NOT NULL,
[QuestionUId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
CONSTRAINT [PK_Question] PRIMARY KEY CLUSTERED ([QuestionId] ASC)
);

CREATE UNIQUE NONCLUSTERED INDEX [Question_QuestionUId_UIX]
ON [dbo].[Question]([QuestionUId] ASC);

CREATE TABLE [dbo].[AdminTestQuestion] (
[AdminTestQuestionId] INT IDENTITY (1, 1) NOT NULL,
[AdminTestId] INT NOT NULL,
[QuestionNumber] INT NOT NULL,
[QuestionUId] UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT [PK_AdminTestQuestion] PRIMARY KEY CLUSTERED ([AdminTestQuestionId] ASC),
CONSTRAINT [FK_AdminTestQuestionQuestionUId] FOREIGN KEY ([QuestionUId]) REFERENCES [dbo].[Question] ([QuestionUId])
);

当我运行 Entity Framework 逆向工程师时,它不会从 AdminTestQuestion > 问题创建映射并且它不会在 Question 类中创建 AdminTestQuestions 的集合。我希望看到这样的东西,但据我了解,当外键的一端是 GUID 时,就会遇到困难。

public partial class Question
{
public Question()
{
this.Answers = new List<AdminTestQuestion>();
}
public int QuestionId { get; set; }
public int ProblemId { get; set; }
public System.Guid QuestionUId { get; set; }
}

我还期望 AdminTestQuestion 映射文件中包含类似这样的映射信息:

        this.HasRequired(t => t.QuestionUId)
.With????(t => t.Questions)
.HasForeignKey(d => d.QuestionUId);

有人可以建议我可以使用 EF6+ 创建这种到 GUID 的映射吗?

最佳答案

EF6 无法执行此操作,因为它不知道 QuestionUId 列上的唯一索引。执行此操作的唯一方法是将 QuestionUId 设为 Question 表中的主键,我认为没有什么可以阻止您这样做:

CREATE TABLE [dbo].[Question] (
[QuestionId] INT IDENTITY (1, 1) NOT NULL,
[ProblemId] INT NOT NULL,
[QuestionUId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
CONSTRAINT [PK_Question] PRIMARY KEY CLUSTERED ([QuestionUId] ASC)
);

CREATE TABLE [dbo].[AdminTestQuestion] (
[AdminTestQuestionId] INT IDENTITY (1, 1) NOT NULL,
[AdminTestId] INT NOT NULL,
[QuestionNumber] INT NOT NULL,
[QuestionUId] UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT [PK_AdminTestQuestion] PRIMARY KEY CLUSTERED ([AdminTestQuestionId] ASC),
CONSTRAINT [FK_AdminTestQuestionQuestionUId] FOREIGN KEY ([QuestionUId]) REFERENCES [dbo].[Question] ([QuestionUId])
);

生成(使用数据库中的 Code First):

public partial class Question
{
public Question()
{
AdminTestQuestion = new HashSet<AdminTestQuestion>();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int QuestionId { get; set; }
public int ProblemId { get; set; }
[Key]
public Guid QuestionUId { get; set; }
public virtual ICollection<AdminTestQuestion> AdminTestQuestion { get; set; }
}

public partial class AdminTestQuestion
{
public int AdminTestQuestionId { get; set; }
public int AdminTestId { get; set; }
public int QuestionNumber { get; set; }
public Guid QuestionUId { get; set; }
public virtual Question Question { get; set; }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Question>()
.HasMany(e => e.AdminTestQuestion)
.WithRequired(e => e.Question)
.WillCascadeOnDelete(false);
}

关于asp.net - Entity Framework 可以生成从外键到父表中 GUID 的映射吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25998601/

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