简体   繁体   中英

Linq insert many to one rows

I would like to insert 10 rows at once into a table that has a many to one relation with another table.

The MainTable.cs file appears as so:

public class MainTable
{
    public int Id { get; set; }
    [Required]
    public Customers Customers { get; set; }
    public string Attribute1 { get; set; }
    public string Attribute2 { get; set; }
}

And the Customers table appears as so:

public class Customers
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
}

I am trying to add 10 rows into the MainTable with 10 different Attribute1 columns, while keeping the Customer the same. But when I try to add it as below, it does not give me the option to add repeated CustomerId:

    public void AddRecord(int customerId)
    {
        IEnumerable<Attributes> Attributes = GetAttributes();
        foreach (var attribute in Attributes)
        {
            _context.Add(new MainTable
            {
                 ***Customers.Id = customerId*** << Customers.Id doesn't exist.
                 Attribute1 = attribute
            });
        }
        _context.SaveChanges();
    }

Anyone know how to do this?

That's because the MainTable class (by the way, that's a terrible name for an entity class) doesn't have a customer ID field. It's expecting a complete Customer objects.

You can either pass the Customer object to the function:

public void AddRecord(Customer customer)
{
    IEnumerable<Attributes> Attributes = GetAttributes();
    foreach (var attribute in Attributes)
    {
        _context.Add(new MainTable
        {
             Customer = customer,
             Attribute1 = attribute
        });
    }
    _context.SaveChanges();
}

Or you can get the Customer object from the ID inside the function:

public void AddRecord(int customerId)
{
    //First get the Customer object. This is an example, modify to your actual code
    Customer customer = GetCustomer(customerId);

    IEnumerable<Attributes> Attributes = GetAttributes();
    foreach (var attribute in Attributes)
    {
        _context.Add(new MainTable
        {
             Customer = customer,
             Attribute1 = attribute
        });
    }
    _context.SaveChanges();
}

You'll notice that I renamed the MainTable.Customers property to Customer . Using a plural noun is confusing. You Customer class is not a list, it's for a single entity, so rename it to singular noun Customer and do the same for the MainTable.Customers property.

If you actually want it to be a list, then change the property to:

public List<Customer> Customers { get; set; }

The Customer class should still be singular.

And then change the assignment line in the functions above to:

_context.Add(new MainTable
{
     Customers = new List<Customer>{ customer },
     Attribute1 = attribute
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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