Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF如何使用对其他表的引用对表进行种子设定_C#_Entity Framework_Seeding - Fatal编程技术网

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

C# EF如何使用对其他表的引用对表进行种子设定,c#,entity-framework,seeding,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 Dep

我有以下型号:

[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; }
}
-

-

我正在为我的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);
    }

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

您可以显式创建用于读取的上下文,您只需正确配置它,然后,您可以将initalizer设置为null,让EF单独进行连接管理、查询创建和对象物化。您可以显式创建用于读取的上下文,您只需正确配置它,然后您可以将initalizer设置为null,让EF单独进行连接管理,查询创建和对象物化。
[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; }
}
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);
    }
 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);
    }