我正在为一所学校的项目工作,我遇到了一对一关系的问题。根据州的模式,它是关于具有某种状态(新的,被接受的等)的提议。双方都需要这种关系。这是我的代码:
用于提案的映射类:
namespace Projecten2Groep23.Models.DAL.Mapper
{
public class ProposalMap : EntityTypeConfiguration<Proposal>
{
public ProposalMap()
{
ToTable("Proposals");
HasKey(p => p.ProposalId);
Property(p => p.ProposalId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.Title);
Property(p => p.KeyWords);
Property(p => p.Goal);
Property(p => p.Plan);
Property(p => p.Problems);
Property(p => p.ResearchArea1);
Property(p => p.ResearchArea2).IsOptional();
HasRequired(p => p.ProposalState).WithRequiredPrincipal().Map(b=> b.MapKey("ProposalStateMapId")).WillCascadeOnDelete(true);
//NotMapped weghalen in proposal.cs als je proposalstate wil mappen
HasOptional(p => p.Feedback).WithRequired().Map(b => b.MapKey("FeedbackMapId")).WillCascadeOnDelete(true);
HasOptional(p => p.CoPromotor).WithRequired().Map(b => b.MapKey("ProposalMapId")).WillCascadeOnDelete(false);
}
}
}
用于提案状态的映射类
namespace Projecten2Groep23.Models.DAL.Mapper
{
public class ProposalStateMap : EntityTypeConfiguration<ProposalState>
{
public ProposalStateMap()
{
ToTable("ProposalState");
HasKey(p => p.ProposalStateId);
Property(p => p.ProposalStateId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasRequired(p => p.Proposal).WithRequiredDependent().Map(b => b.MapKey("ProposalMapId")).WillCascadeOnDelete(true);
}
}
}
现在,在调用context.SaveChanges时,我不断收到标题中声明的异常。我几乎尝试了这种关系的所有配置,但它仍然没有被映射。
我已经在stackoverflow上搜索了十几篇帖子,但没有用。
编辑:有人要求保存操作
namespace Projecten2Groep23.Models.DAL
{
public class BachProefRegistrationInitializer : DropCreateDatabaseAlways<BachProefRegistratieContext>
{
protected override void Seed(BachProefRegistratieContext context)
{
try
{
Student student = new Student("student", "student@student.be", true, "student");
Proposal proposal = new Proposal("lol", "keywords", "plan", "problems", "research", null, "title", new Copromotor("copromotor", "copromotor@copromotor.be", "organisatie copromotor"), 0, "referencelist", DateTime.Now);
proposal.ProposalState = new AdviceBPCState(proposal);
proposal.Feedback = new Feedback();
student.AddProposal(proposal);
BPCoordinator bpc = new BPCoordinator("bpc", "bpc@bpc.be", true, "bpc");
ICollection<Student> students = new List<Student>();
students.Add(student);
Promotor promotor = new Promotor("promotor", "promotor@promotor.be", false, "promotor", students);
Student student2 = new Student("pieter", "p2groep23@gmail.com", false, "pieter");
List<User> users = (new User[] {student, bpc, promotor, student2}).ToList();
context.Proposals.Add(proposal);
users.ForEach(u => context.Users.Add(u));
context.SaveChanges();
}
catch (DbEntityValidationException e)
{
string s = "Fout creatie database ";
foreach (var eve in e.EntityValidationErrors)
{
s += String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.GetValidationResult());
foreach (var ve in eve.ValidationErrors)
{
s += String.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
}
}
throw new Exception(s);
}
}
}