EF如何使用对其他表的引用对表进行种子设定

时间:2016-04-15 21:45:56

标签: c# entity-framework seeding

我有以下型号:

[Table("SGDB_Persons")]
public class Person {
    public int Id { get; set; }

    [Required]
    public string Firstname { get; set; }

    [Required]
    public string Lastname { get; set; }

    [Required]
    public virtual Department Department { get; set; }

    [Required]
    public SourceType SourceType { get; set; }

    [Required]
    public virtual PersonData PersonData { get; set; }
}

-

[Table("SGDB_Departments")]
public class Department {
    public int Id { get; set; }

    [Required]
    public string Costcenter { get; set; }

    [Required]
    public string Abbreviation { get; set; }

    public string Description { get; set; }

    public string FullDepartmentName {
        get {
            return $@"{Division.Abbreviation}\{Abbreviation}";
        }
    }
    [Required]
    public virtual Division Division { get; set; }

    public override string ToString() {
        return FullDepartmentName;
    }
}

-

[Table("SGDB_Users")]
public class User {
    public int Id { get; set; }

    [Required]
    public string UserID { get; set; }

    public string Domain { get; set; }

    [Required]
    public Person Person { get; set; }

    [Required]
    public SourceType SourceType { get; set; }
}

我用这段代码播种我的Person(及相关)表:

protected override void Seed(PersonContext context) {
        var dep = new Department() {
            Abbreviation = "ICO2",
            Costcenter = "890",
            Description = "Information, Coordination and Organization 2",
            Division = new Division() {
                Abbreviation = "SG",
                Description = "Startermotors and Generators",
                PayrollArea = "220"
            }
        };
        var status = new Status {
            StatusType = "Test"
        };
        var persondata = new PersonData {
            Status = status,
            Limit = new DateTime(1900, 1, 1),
            Responsible = null
        };
        for (int i = 0; i < 20; i++) {
            var person = new Person {
                Firstname = $"TestPersonFirstname{i}",
                Lastname = $"TestPersonLastname{i}",
                SourceType = COM.SourceType.Manual,
                Department = dep,
                PersonData = persondata
            };
            context.Persons.Add(person);
        }
        context.SaveChanges();
        base.Seed(context);
    }

过去我尝试在播种时从数据库中检索一些数据(例如Departmens)(创建DepartmentContext的新实例并调用Get方法)。这创建了元数据异常。正如您所看到的,我通过在人员播种中创建所有必需的数据来解决这个问题。

现在我想为一些用户播种 - 用户模型包含一个Person作为导航属性。我想做这样的事情:

 protected override void Seed(UserContext context) {

        var person = new PersonContext().Persons.First();

        var user = new User() {
            Domain = "DE",
            Person = person,
            SourceType = COM.SourceType.Manual,
            UserID = "ThisIsATest"
        };
        context.Users.Add(user);

        base.Seed(context);
    }

但正如我所说,这引发了一个例外。如果可能的话,我不想将整个人员播种过程包含在用户播种过程中 - 而是希望从数据库中获取第一个数据集(如代码示例)

0 个答案:

没有答案