0

所以,我的 ef 模型有以下初始化程序。

为简化起见,我有一个车辆、一个客户、一个地址实体。我用一种方法创建了地址实体。现在我需要创建客户实体,但如何将地址值分配给现有对象?

public class DevelopmentInitializer
    {
        /// <summary>
        /// This class allows the database tables to be created and seed at runtime.
        /// </summary>
        public class AskAndTrackInitializer : DropCreateDatabaseAlways<AskAndTrackContext>
        {
            /// <summary>
            /// This fills the database with sample data to start with
            /// </summary>
            /// <param name="context">DBContext required for the connection</param>
            protected override void Seed(AskAndTrackContext context)
            {
                BuildDealers(context);
                BuildRequesters(context);
                BuildAddresses(context);
                BuildCustomers(context);
                BuildVehicles(context);

                BuildWorkflowsBase(context);
            }

            private void BuildDealers(AskAndTrackContext context)
            {
                var dealer1 = new Dealer {DealerId = 1, Name = "Dealer 1"};
                var dealer2 = new Dealer { DealerId = 2, Name = "Dealer 2" };
                var dealer3 = new Dealer { DealerId = 3, Name = "Dealer 3" };
                var dealer4 = new Dealer { DealerId = 4, Name = "Dealer 4" };
                var dealer5 = new Dealer { DealerId = 5, Name = "Dealer 5" };

                context.Dealers.Add(dealer1);
                context.Dealers.Add(dealer2);
                context.Dealers.Add(dealer3);
                context.Dealers.Add(dealer4);
                context.Dealers.Add(dealer5);

            }

            /// <summary>
            /// Requesters test data
            /// </summary>
            private void BuildRequesters(AskAndTrackContext context)
            {
                var requester1 = new Requester {EmailAddress = "le.xx@hotmail.com", Name="Andres x", RequesterId=1};
                var requester2 = new Requester { EmailAddress = "le.xxx@hotmail.com", Name = "Andres y", RequesterId = 2 };
                var requester3 = new Requester { EmailAddress = "le.yyy@hotmail.com", Name = "Juan l", RequesterId = 3 };
                var requester4 = new Requester { EmailAddress = "le.zzzz@hotmail.com", Name = "Peter x", RequesterId = 4 };
                var requester5 = new Requester { EmailAddress = "le.aaaa@hotmail.com", Name = "Patrick z", RequesterId = 5 };

                context.Requesters.Add(requester1);
                context.Requesters.Add(requester2);
                context.Requesters.Add(requester3);
                context.Requesters.Add(requester4);
                context.Requesters.Add(requester5);

            }

            /// <summary>
            /// Workflow base test data and its related objects
            /// </summary>
            private void BuildWorkflowsBase(AskAndTrackContext context)
            {

            }

            /// <summary>
            /// Vehicle test data
            /// </summary>
            private void BuildVehicles(AskAndTrackContext context)
            {
                //var vehicle1
            }

            /// <summary>
            /// Customers test data
            /// </summary>
            private void BuildCustomers(AskAndTrackContext context)
            {
                //var customer1 = new Customer{Address = ???


            }

            private void BuildAddresses(AskAndTrackContext context)
            {
                var address1 = new Address
                                    {
                                        AddressId = 1,
                                        Box = "71",
                                        City = "Antwerp",
                                        Number = "1",
                                        Street = "Belgielei",
                                        Zip = "2018"
                                    };

                var address2 = new Address
                                    {
                                        AddressId = 2,
                                        Box = "21",
                                        City = "Antwerp",
                                        Number = "1",
                                        Street = "Lange Leemstraat",
                                        Zip = "2018"
                                    };

                var address3 = new Address
                                    {
                                        AddressId = 3,
                                        Box = "23",
                                        City = "Antwerp",
                                        Number = "1",
                                        Street = "Lamoriniestraat",
                                        Zip = "2018"
                                    };

                var address4 = new Address
                                    {
                                        AddressId = 4,
                                        Box = "24",
                                        City = "Antwerp",
                                        Number = "1",
                                        Street = "De Keyserlei",
                                        Zip = "2000"
                                    };

                var address5 = new Address
                                    {
                                        AddressId = 5,
                                        Box = "34",
                                        City = "Antwerp",
                                        Number = "1",
                                        Street = "Italialei",
                                        Zip = "2020"
                                    };

                context.Addresses.Add(address1);
                context.Addresses.Add(address2);
                context.Addresses.Add(address3);
                context.Addresses.Add(address4);
                context.Addresses.Add(address5);

            }
        }
4

2 回答 2

1

在 EF Code First 中,您必须声明两个实体之间的关系,并声明子实体中的子实体列表

public List<ChildeEntity> ChildEntities { get; set; }

然后,如果您创建一个 ChildEntity:

var newChild = new ChildEntity();

并将其添加到父实体列表中:

parentEntity.ChildEntities.Add(newChild);

如果父实体在上下文中,则 newChild 将自动放入上下文中,并且当您更改更改时,将在 newChild 中设置子实体的必要属性(FK 属性),然后将更改保存到数据库中。

因此,答案是:只需将其添加到父列表中即可。

实际上,这种关系可以通过 3 种方式表达:

  • 父级的子实体列表
  • 子实体中指向父实体的属性
  • FK(可以包含在实体中或不包含在实体中)

如果您在上下文中工作,则无论何时更改 3 个属性中的任何一个,都会在上下文中相应地更正其他属性。

所以答案可以包括更改任何其他属性。

于 2012-05-08T13:26:39.237 回答
0

我看到两个选项:(与 lambda 表达式无关;)

  1. 你可以BuildCustomers从里面打电话BuildAdresses

    private void BuildAddresses(AskAndTrackContext context)
    {
        var listAddress = new List<Address>();
        // create adresses, populate listAdress with the Address objects. 
        BuildCustomers(AskAndTrackContext context, listAddress);
    }
    
  2. BuildAdresses可以返回 ID 列表,并将列表作为参数传递给BuildCustomers.

    List<Address> addresses = BuildAddresses(context);
    BuildCustomers(context, addresses);
    

不要忘记context.SaveChanges()在每个填充函数的末尾调用。这样 Entity 就会知道您传递的对象是表中的一个元素。

两种解决方案都会做同样的事情,但可读性会有所不同。

于 2012-05-08T13:08:28.290 回答