C# 实体框架6.1.2:零或一对多
下面的代码解析XML并将值存储到实体框架对象中,这些对象最终会存储到SQL数据库中: Program.cs:C# 实体框架6.1.2:零或一对多,c#,linq,entity-framework-6,C#,Linq,Entity Framework 6,下面的代码解析XML并将值存储到实体框架对象中,这些对象最终会存储到SQL数据库中: Program.cs: namespace locationProject { class Program { static void Main(string[] args) { XDocument xml = XDocument.Load("locationDoc.xml"); XNamespace cm = "h
namespace locationProject
{
class Program
{
static void Main(string[] args)
{
XDocument xml = XDocument.Load("locationDoc.xml");
XNamespace cm = "http://somenamespace.com/cm";
List<Location> locationColl = new List<Location>();
List<Event> eventColl = new List<Event>();
locationModelContainer context = new locationModelContainer();
var i = 0;
var i2 = 0;
foreach (var continent in xml.Descendants(cm + "Continent"))
{
Location l = new Location();
locationColl.Add(l);
locationColl[i].Continent = (string)continent.Element(cm + "Name");
foreach (var country in continent.Elements(cm + "Country"))
{
locationColl[i].Country = (string)country.Element(cm + "Name");
foreach (var city in country.Elements(cm + "City"))
{
locationColl[i].City = (string)city.Element(cm + "Name");
context.Locations.Add(locationColl[i]);
foreach (var events in city.Elements(cm + "Event"))
{
Event e = new Event();
eventColl.Add(e);
eventColl[i2].Name = (string)events.Element(cm + "Name");
eventColl[i2].Date = (string)events.Element(cm + "Date");
context.Events.Add(eventColl[i2]);
i2++;
}
i++;
}
}
}
context.SaveChanges();
}
}
}
使用一对多关系(一个位置对多个事件)可以如预期的那样工作。Location\u LocationID
外键将填充相关的LocationID
尽管如此,如上代码所示,我的预期设计是使用零或一对多关系(零或一个位置对多个事件)。但是,使用此关系不会填充Location\u LocationID
外键,而是在保存到数据库时,所有Location\u LocationID
字段都包含NULL
有人能解释一下为什么他们不使用这种关系吗?提前谢谢 您当前未设置任何事件的位置。所以它当然总是空的。您也只在每个大陆创建一个位置。将创建位置的位置移动到枚举城市的位置
foreach (var continent in xml.Descendants(cm + "Continent"))
{
var continent = (string)continent.Element(cm + "Name");
foreach (var country in continent.Elements(cm + "Country"))
{
var country = (string)country.Element(cm + "Name");
foreach (var city in country.Elements(cm + "City"))
{
Location l = new Location();
l.Continent = continent;
l.Country = country;
l.City = (string)city.Element(cm + "Name");
context.Locations.Add(l);
locationColl.Add(l);
foreach (var events in city.Elements(cm + "Event"))
{
Event e = new Event();
eventColl.Add(e);
e.Location = l;
eventColl[i2].Name = (string)events.Element(cm + "Name");
eventColl[i2].Date = (string)events.Element(cm + "Date");
context.Events.Add(eventColl[i2]);
i2++;
}
i++;
}
}
}
context.SaveChanges();
谢谢你的回复。很抱歉问,但我如何才能做到这一点?@Cryptonaut我更新了答案。。。但后来我意识到你的密码有点坏了。你只会在每个大陆而不是每个城市找到一个位置。您在错误的点创建位置。
namespace locationProject
{
using System;
using System.Collections.Generic;
public partial class Location
{
public Location()
{
this.Events = new HashSet<Event>();
}
public int LocationID { get; set; }
public string Continent { get; set; }
public string Country { get; set; }
public string City { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
}
namespace locationProject
{
using System;
using System.Collections.Generic;
public partial class Event
{
public int EventID { get; set; }
public Nullable<int> Location_LocationID { get; set; }
public string Name { get; set; }
public string Date { get; set; }
public virtual Location Location { get; set; }
}
}
public partial class locationModelContainer : DbContext
{
public locationModelContainer()
: base("name=locationModelContainer")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Location> Locations { get; set; }
public virtual DbSet<Event> Events { get; set; }
}
}
SET QUOTED_IDENTIFIER OFF;
GO
USE [Locations];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO
IF OBJECT_ID(N'[dbo].[FK_LocationEvent]', 'F') IS NOT NULL
ALTER TABLE [dbo].[Events] DROP CONSTRAINT [FK_LocationEvent];
GO
IF OBJECT_ID(N'[dbo].[Locations]', 'U') IS NOT NULL
DROP TABLE [dbo].[Locations];
GO
IF OBJECT_ID(N'[dbo].[Events]', 'U') IS NOT NULL
DROP TABLE [dbo].[Events];
GO
CREATE TABLE [dbo].[Locations] (
[LocationID] int IDENTITY(1,1) NOT NULL,
[Continent] nvarchar(max) NOT NULL,
[Country] nvarchar(max) NOT NULL,
[City] nvarchar(max) NOT NULL
);
GO
CREATE TABLE [dbo].[Events] (
[EventID] int IDENTITY(1,1) NOT NULL,
[Location_LocationID] int NULL,
[Name] nvarchar(max) NOT NULL,
[Date] nvarchar(max) NOT NULL
);
GO
ALTER TABLE [dbo].[Locations]
ADD CONSTRAINT [PK_Locations]
PRIMARY KEY CLUSTERED ([LocationID] ASC);
GO
ALTER TABLE [dbo].[Events]
ADD CONSTRAINT [PK_Events]
PRIMARY KEY CLUSTERED ([EventID] ASC);
GO
ALTER TABLE [dbo].[Events]
ADD CONSTRAINT [FK_LocationEvent]
FOREIGN KEY ([Location_LocationID])
REFERENCES [dbo].[Locations]
([LocationID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO
CREATE INDEX [IX_FK_LocationEvent]
ON [dbo].[Events]
([Location_LocationID]);
GO
foreach (var continent in xml.Descendants(cm + "Continent"))
{
var continent = (string)continent.Element(cm + "Name");
foreach (var country in continent.Elements(cm + "Country"))
{
var country = (string)country.Element(cm + "Name");
foreach (var city in country.Elements(cm + "City"))
{
Location l = new Location();
l.Continent = continent;
l.Country = country;
l.City = (string)city.Element(cm + "Name");
context.Locations.Add(l);
locationColl.Add(l);
foreach (var events in city.Elements(cm + "Event"))
{
Event e = new Event();
eventColl.Add(e);
e.Location = l;
eventColl[i2].Name = (string)events.Element(cm + "Name");
eventColl[i2].Date = (string)events.Element(cm + "Date");
context.Events.Add(eventColl[i2]);
i2++;
}
i++;
}
}
}
context.SaveChanges();