0
的保存有多個實體在微風和EF代碼首先導致兩個額外的空值的實體一對多的關係,的保存有多個實體一對多關係在微風和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();
}]);
}());
您正在爲'EntityManager.createEntity'提供initalValues參數。該對象有一個「服務器」屬性,它提供足夠的信息來自動將新創建的ServerAddress實體與其父服務器實體連接起來。 'self.manager.addEntity'和'server.addresses.push'行是不必要的。要確認一切工作正常,請在'createEntity'行後面設置一個斷點並檢查server.addresses數組。它應該包含新的ServerAddress實體。 –