在微风和EF代码中首先保存具有一对多关系的多个实体

时间:2014-12-29 12:10:13

标签: entity-framework ef-code-first breeze

在微风和EF代码中保存具有一对多关系的多个实体首先会产生两个额外的空值实体,

我做错了什么?

这是我的代码(我从帖子中删除了所有不相关的实体和脚本)

实体

public class Server
{
    public int Id { get; set; }

    public ICollection<ServerAddress> Addresses { get; set; }

    public string Name { get; set; }

    public int DomainId { get; set; }

    public Domain Domain { get; set; }
}

public class ServerAddress
{
    public int Id { get; set; }

    public string Ip { get; set; }

    public ICollection<ServerAddressFarm> ServerAddressFarms { get; set; }

    public int ServerId { get; set; }

    public Server Server { get; set; }
}

的DbContext

public class PortalDbContext : DbContext
{
    public DbSet<Server> Servers { get; set; }
    public DbSet<ServerAddress> ServerAddresses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Server PK
        modelBuilder.Entity<Server>()
            .HasKey(t => t.Id)
            .Property(t=>t.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

         // ServerAddress PK
        modelBuilder.Entity<ServerAddress>()
            .HasKey(t => t.Id)
            .Property(t => t.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);


        // Domain 1-->* Server
        modelBuilder.Entity<Server>()
            .HasRequired(t => t.Domain)
            .WithMany(t => t.Servers)
            .WillCascadeOnDelete(false);
            //.HasForeignKey(t => t.DomainId);

        // Server  1-->* ServerAddress
        modelBuilder.Entity<ServerAddress>()
            .HasRequired(t => t.Server)
            .WithMany(t => t.Addresses)
            .WillCascadeOnDelete(false);


    }
}

角度服务

(function () {
"use strict";

angular.module('portalApp')
    .service('serversService', [
        '$q', '$http',
        function ($q, $http) {
            var self = this;

            breeze.NamingConvention.camelCase.setAsDefault();
            self.manager = new breeze.EntityManager('breeze/Servers');

            self.addServer = function(server, serverData) {

                var serverEntity = self.manager.createEntity('Server', server);
                self.manager.addEntity(serverEntity);

                if (serverData.ip1) {
                    serverData.ip1 = self.manager.createEntity('ServerAddress', { ip: serverData.ip1, server: serverEntity });
                    self.manager.addEntity(serverData.ip1);
                    server.addresses.push({ ip: serverData.ip1 });

                }

                if (serverData.ip2) {
                    serverData.ip2 = self.manager.createEntity('ServerAddress', { ip: serverData.ip2, server: serverEntity });
                    self.manager.addEntity(serverData.ip2);
                    server.addresses.push({ ip: serverData.ip2 });
                }

                return self.manager.saveChanges();
            };
        }
    ]);
}());

角度控制器

(function () {
"use strict";
angular.module('portalApp')
    .controller('serverListCtrl', ['$rootScope', '$scope', 'serversService', 'notificationFactory',
        function ($rootScope, $scope, serversService, notificationFactory) {
            var vm = this;

            vm.addServer = function() {
                serversService.addServer(vm.server, vm.serverData)
                    .then(
                        function (data) {
                            vm.server = { name: null, addresses: [] };
                            vm.serverData = { ip1: null, ip2: null };
                            notificationFactory.success("server was added");
                            vm.getServers(vm.domain);
                        })
                    .fail(
                        function (reason) {
                            notificationFactory.errors(reason);
                        });
            };


            vm.init = function () {
                vm.domain = null;
                vm.server = {name: null, addresses: [{},{}]};
                vm.serverData = { ip1: null, ip2: null};
            };

            vm.init();
        }]);
}());

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

删除不必要的self.manager.addEntity和server.addresses.push行解决了这个问题, 谢谢Jeremy Danyow

            self.addServer = function(server, serverData) {

                var serverEntity = self.manager.createEntity('Server', server);
                self.manager.addEntity(serverEntity);

                if (serverData.ip1) {
                    serverData.ip1 = self.manager.createEntity('ServerAddress', { ip: serverData.ip1, server: serverEntity });
                    //self.manager.addEntity(serverData.ip1);
                    //server.addresses.push({ ip: serverData.ip1 });

                }

                if (serverData.ip2) {
                    serverData.ip2 = self.manager.createEntity('ServerAddress', { ip: serverData.ip2, server: serverEntity });
                    //self.manager.addEntity(serverData.ip2);
                    //server.addresses.push({ ip: serverData.ip2 });
                }



                return self.manager.saveChanges();
            };