简体   繁体   English

一对多关系EF 6

[英]Add One to many relationship EF 6

I'm working on an application for printers auditing. 我正在为打印机审核应用程序。 So, when an user choose a printer name, he could see all the users which are related to it. 因此,当用户选择打印机名称时,他可以看到与其相关的所有用户。 This is my database model : 这是我的数据库模型:

在此处输入图片说明

I'm thinking about built a little script for sending user name account, pc name, list of printers and the datetime to a WCF service from user's PCs. 我正在考虑构建一个小脚本,用于从用户的PC向WCF服务发送用户名帐户,PC名称,打印机列表和日期时间。 Then a WCF service could upload these info in database. 然后,WCF服务可以将这些信息上载到数据库中。

But... I have a problem to understand how I could create a new record with a one to many relationship. 但是...我有一个问题要了解如何建立具有一对多关系的新记录。

public void AuditRecord(DateTime date,String user,string[] printer, String pc)
{
var USR = new Users ()
{
    UserName = user,
};

var PC = new Pc()
{
    PcName = pc,
};

var PTR = new Printers();
List<Printers> ptr = new List<Printers>();
for (int i = 0;i<printer.Count;i++)
{
    ptr.Add(new Printers
    {
        PrinterName= printer[i],
    });
}

var REF = new Reference()
{
Date = date,
};

context.SaveChanges(); }

EDIT : 编辑:

With this solution it's working for insertion. 使用此解决方案,它可以插入。

ptr.ToList().ForEach(p=>
{
    var RF = new Reference() {Date=DateTime.Now};
    var USR = new Users() {UserName=user};
    var PC = new Pc() {PcName=pc};
    var PTR = new Printers() {PrinterName = p };
    PTR.Reference.Add(RF);
    USR.Reference.Add(RF);
    PC.Reference.Add(RF);
    context.Pc.Add(PC);
    context.Users.Add(USR);
    context.Printers.Add(PTR);
});
context.SavesChanges();

But I have a problem in reference table. 但是我在参考表中有问题。 If an user use three printers, I need to insert the Id's for each printer and the same Id's for User and Pc. 如果用户使用三台打印机,则需要为每台打印机插入ID,并为User和Pc插入相同的ID。 Like this 像这样

Date     | PrinterId | UserId | PcId
17/11/15        1         1       1
17/11/15        2         1       1
17/11/15        3         1       1

And I have : 我有 :

Date     | PrinterId | UserId | PcId
17/11/15        1         1       1
17/11/15        2         2       2
17/11/15        3         3       3

Referring to your diagram, you should have a similar model like this: 参考您的图,您应该具有类似的模型,如下所示:

class Reference
{
    public Reference()
    {
        Printers = new HashSet<Printers>();
        Pc = new HashSet<Pc>();
        Users = new HashSet<Users>();
    }

    public DateTime Date { get; set; }
    public ICollection<Printers> Printers { get; set; }
    public ICollection<Pc> Pc { get; set; }
    public ICollection<Users> Users { get; set; }
}

You can add the relationships by using the .Add() method like the following: 您可以使用.Add()方法添加关系,如下所示:

    public void AuditRecord(DateTime date, String user, string[] printer, String pc)
    {
        // enumerate through printer names because each Reference only has 1 printer
        printer.ToList().ForEach(p =>
        {
            // instantiate the instances
            var USR = new Users() { UserName = user };
            var PC = new Pc() { PcName = pc };
            var PTR = new Printers() { PrinterName = p }
            var REF = new Reference() { Date = date };

            REF.Users.Add(USR); // add User to Reference
            REF.Pc.Add(PC); // add PC to Reference
            REF.Printers.Add(PTR); // add Printer to Reference

            // add new instances into context for saving
            context.Set<Users>().Add(USR); 
            context.Set<Pc>().Add(PC); 
            context.Set<Printers>().Add(PTR); 
            context.Set<Reference>().Add(REF); 
        });

        context.SaveChanges(); 
    }

Finally I have the solution : 最后我有解决方案:

var USR = new Users() {UserName=user};
var PC = new Pc() {PcName=pc};    
ptr.ToList().ForEach(p=>
    {
        var RF = new Reference() {Date=DateTime.Now};
        var PTR = new Printers() {PrinterName = p };
        PTR.Reference.Add(RF);
        USR.Reference.Add(RF);
        PC.Reference.Add(RF);
        context.Set<Pc>().Add(PC);
        context.Set<Users>().Add(USR);
        context.Set<Printers>().Add(PTR);
    });
    context.SavesChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM