0
我需要在數據庫中創建一些虛擬記錄,但是當我使用下面的代碼填充數據庫時,它不會創建任何內容。實體框架,種子和身份
我沒有得到任何錯誤,我可以看到種子方法正在運行。
我懷疑它與身份和自動增量有關,但我不太確定。
我有一些遞歸表和表之間的關係,所以我怎麼能在我的種子方法中處理這個?
namespace CalMeser.Data.Sql.Migrations
{
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Diagnostics.Contracts;
using CalMeser.Data.Entities;
[ContractVerification(false)]
internal sealed class Configuration : DbMigrationsConfiguration<DataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(DataContext context)
{
context.Tags.AddOrUpdate(new Tag
{
Name = "Computers",
Sort = 1,
Children = new List<Tag>
{
new Tag
{
Name = "Computer Science",
Sort = 1,
Children = new List<Tag>
{
new Tag
{
Name = "Programming",
Sort = 4
},
new Tag
{
Name = "Security",
Sort = 2
},
new Tag
{
Name = "Algorithms",
Sort = 3
},
new Tag
{
Name = "Data Structures",
Sort = 1
},
}
},
new Tag
{
Name = "Computer Information Systems",
Sort = 2
},
new Tag
{
Name = "Computer Engineering",
Sort = 3
}
}
});
context.Tags.AddOrUpdate(new Tag
{
Name = "Nature",
Sort = 1,
Children = new List<Tag>
{
new Tag
{
Name = "Animals",
Sort = 3,
Children = new List<Tag>
{
new Tag
{
Name = "Cats",
Sort = 1
},
new Tag
{
Name = "Dogs",
Sort = 2
},
new Tag
{
Name = "Fish",
Sort = 3
},
new Tag
{
Name = "Monkeys",
Sort = 4
},
}
},
new Tag
{
Name = "Plants",
Sort = 2,
Children = new List<Tag>
{
new Tag
{
Name = "Mint",
Sort = 1
},
new Tag
{
Name = "Sage",
Sort = 2
},
new Tag
{
Name = "Lime",
Sort = 3
}
}
},
new Tag
{
Name = "Space",
Sort = 1
}
}
});
context.SaveChanges();
}
}
}
以下是在上面的代碼中使用的實體。
namespace CalMeser.Data.Abstractions
{
public abstract class Entity : IEntity
{
public long Id { get; set; }
}
}
namespace CalMeser.Data.Abstractions
{
using System.Collections.Generic;
public abstract class RecursiveEntity<TEntity> : Entity where TEntity : RecursiveEntity<TEntity>
{
public virtual IList<TEntity> Children { get; set; }
public virtual TEntity Parent { get; set; }
public long? ParentId { get; set; }
}
}
namespace CalMeser.Data.Entities
{
using CalMeser.Data.Abstractions;
public class Tag : RecursiveEntity<Tag>
{
public File Image { get; set; }
public string Name { get; set; }
public long Sort { get; set; }
}
}