我有以下内容:
public class Comment : IEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("userId")]
public Guid UserId { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("eventId")]
public Guid EventId { get; set; }
[JsonProperty("date")]
public DateTimeOffset DateCreated { get; set; }
[JsonProperty("id")]
public DateTimeOffset DateModified { get; set; }
public virtual Event Event { get; set; }
}
public class Event : IEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("date")]
public DateTimeOffset Date { get; set; }
[JsonProperty("distance")]
public int Distance { get; set; }
[JsonProperty("verticalAscend")]
public int VerticalAscend { get; set; }
[JsonProperty("userId")]
public Guid UserId { get; set; }
//attending
public DateTimeOffset DateCreated { get; set; }
public DateTimeOffset DateModified { get; set; }
[JsonProperty("user")]
public virtual User User { get; set; }
[JsonProperty("comments")]
public virtual ICollection<Comment> Comments { get; set; }
[JsonProperty("attending")]
public virtual ICollection<User> AttendingList { get; set; }
}
public class User : IEntity
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("profilePicUrl")]
public string ProfilePicUrl { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("surname")]
public string LastName { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
public DateTimeOffset DateCreated { get; set; }
public DateTimeOffset DateModified { get; set; }
public virtual ICollection<Event> AttendingEvents { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
我在Fluent API
类中具有以下EventConfig
配置,这些配置继承自EntityTypeConfiguration<Event>
。
public class EventConfig : EntityTypeConfiguration<Event>
{
public EventConfig()
{
HasRequired(r => r.User)
.WithMany(m => m.Events)
.HasForeignKey(f => f.UserId)
.WillCascadeOnDelete(false);
HasMany(x => x.Comments)
.WithRequired(c => c.Event)
.HasForeignKey(c => c.EventId)
.WillCascadeOnDelete(false);
HasMany(x => x.AttendingList)
.WithMany(u => u.AttendingEvents)
.Map(m =>
{
m.MapLeftKey("EventId");
m.MapRightKey("UserId");
m.ToTable("AttendeeList");
});
}
}
根据我的Fluent API
设置,以下迁移脚本是错误的。
public partial class initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Comments",
c => new
{
Id = c.Guid(nullable: false, identity: true),
UserId = c.Guid(nullable: false),
Text = c.String(),
EventId = c.Guid(nullable: false),
DateCreated = c.DateTimeOffset(nullable: false, precision: 7),
DateModified = c.DateTimeOffset(nullable: false, precision: 7),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Events", t => t.EventId, cascadeDelete: true)
.Index(t => t.EventId);
CreateTable(
"dbo.Events",
c => new
{
Id = c.Guid(nullable: false, identity: true),
Description = c.String(),
Date = c.DateTimeOffset(nullable: false, precision: 7),
Distance = c.Int(nullable: false),
VerticalAscend = c.Int(nullable: false),
UserId = c.Guid(nullable: false),
DateCreated = c.DateTimeOffset(nullable: false, precision: 7),
DateModified = c.DateTimeOffset(nullable: false, precision: 7),
User_Id = c.Guid(),
User_Id1 = c.Guid(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Users", t => t.User_Id)
.ForeignKey("dbo.Users", t => t.User_Id1)
.ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.User_Id)
.Index(t => t.User_Id1);
CreateTable(
"dbo.Users",
c => new
{
Id = c.Guid(nullable: false, identity: true),
ProfilePicUrl = c.String(),
Name = c.String(),
LastName = c.String(),
Email = c.String(),
DateCreated = c.DateTimeOffset(nullable: false, precision: 7),
DateModified = c.DateTimeOffset(nullable: false, precision: 7),
Event_Id = c.Guid(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Events", t => t.Event_Id)
.Index(t => t.Event_Id);
}
public override void Down()
{
DropForeignKey("dbo.Events", "UserId", "dbo.Users");
DropForeignKey("dbo.Comments", "EventId", "dbo.Events");
DropForeignKey("dbo.Users", "Event_Id", "dbo.Events");
DropForeignKey("dbo.Events", "User_Id1", "dbo.Users");
DropForeignKey("dbo.Events", "User_Id", "dbo.Users");
DropIndex("dbo.Users", new[] { "Event_Id" });
DropIndex("dbo.Events", new[] { "User_Id1" });
DropIndex("dbo.Events", new[] { "User_Id" });
DropIndex("dbo.Events", new[] { "UserId" });
DropIndex("dbo.Comments", new[] { "EventId" });
DropTable("dbo.Users");
DropTable("dbo.Events");
DropTable("dbo.Comments");
}
}
如果我删除EventConfig
中的代码,并将其放在OnModelCreating
内,它将正确创建关系。
我删除旧迁移脚本的过程如下:
我的问题是,我的旧迁移脚本是否仍在应用,而不是新迁移,因为此方案不能反映最新的关系更改。