EntityCollections设置一个EntityReference.EntityKey来填充集合 [英] EntityCollections setting an EntityReference.EntityKey to populate the Collection
问题描述
遇到问题与实体框架。我一直在填充EntityReferences用的EntityKey序,以消除查询数据库曾经一次我添加一个实体引用的需要。现在,我想要做同样的事情的协会(EntityCollection),但林不知道从哪里开始。下面是我所试图做的。
表
CREATE TABLE [DBO]。[美孚(
[FooId] [INT] IDENTITY(1,1)NOT NULL,
[FooName] [为nvarchar(50)NOT NULL,
)
CREATE TABLE [DBO]。[酒吧(
[BarId] [INT] IDENTITY(1,1)NOT NULL,
[BarName] [为nvarchar(50)NOT NULL,
约束[PK_Bar] PRIMARY KEY CLUSTERED
)
CREATE TABLE [DBO]。[FooBar的(
[FooId] [INT] NOT NULL,
[BarId] [INT] NOT NULL,
约束[PK_FooBar] PRIMARY KEY CLUSTERED
)
ALTER TABLE [DBO]。[FooBar的] WITH CHECK ADD CONSTRAINT [FK_FooBar_Bar]外键([BarId])
号[DBO]。[酒吧]([BarId])
ALTER TABLE [DBO]。[FooBar的]检查约束[FK_FooBar_Bar]
ALTER TABLE [DBO ]。[FooBar的] WITH CHECK ADD CONSTRAINT [FK_FooBar_Foo]外键([FooId])
参考文献[DBO]。[美孚]([FooId])
ALTER TABLE [DBO]。 [FooBar的]检查约束[FK_FooBar_Foo]
现在,当我映射该给的EntityFramework我得到2实体( Foo和Bar)有多对多的关系。
什么即时试图做的是增加5条相关的
新富实体我的ID酒吧,但我并不想查询数据库,获得所有5个酒吧。我宁愿只设置的EntityKey
这是可能的。
例如:
美孚myFoo =新的Foo();
Foo.Name =你好
Foo.Bars = //我现在想只填充了一套EntityKeys为EntityCollection
的
只是附加的存根实体:
VAR栏=新的酒吧{ID = barId};
Context.AttachTo(酒吧,酒吧);
myFoo.Bars.Add(巴);
Context.SaveChanges();
没有DB查询那里栏
。
重要:您必须将栏
的前的加入关系 myFoo
,使EF知道你不是要插入一个新的酒吧
。
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder to eliminate the need for querying the database ever time I add an Entity Reference. Now I want to do the same thing for an Association (EntityCollection) but im not sure where to start. below are the details of what I am trying to do
Tables
CREATE TABLE [dbo].[Foo](
[FooId] [int] IDENTITY(1,1) NOT NULL,
[FooName] [nvarchar](50) NOT NULL,
)
CREATE TABLE [dbo].[Bar](
[BarId] [int] IDENTITY(1,1) NOT NULL,
[BarName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Bar] PRIMARY KEY CLUSTERED
)
CREATE TABLE [dbo].[FooBar](
[FooId] [int] NOT NULL,
[BarId] [int] NOT NULL,
CONSTRAINT [PK_FooBar] PRIMARY KEY CLUSTERED
)
ALTER TABLE [dbo].[FooBar] WITH CHECK ADD CONSTRAINT [FK_FooBar_Bar] FOREIGN KEY([BarId])
REFERENCES [dbo].[Bar] ([BarId])
ALTER TABLE [dbo].[FooBar] CHECK CONSTRAINT [FK_FooBar_Bar]
ALTER TABLE [dbo].[FooBar] WITH CHECK ADD CONSTRAINT [FK_FooBar_Foo] FOREIGN KEY([FooId])
REFERENCES [dbo].[Foo] ([FooId])
ALTER TABLE [dbo].[FooBar] CHECK CONSTRAINT [FK_FooBar_Foo]
Now when I map this to the entityFramework I get 2 entities (Foo and Bar) with a Many to Many Relationship.
What Im trying to do is add a new Foo entity with 5 Bars associated I have the IDs of the Bars but I do not want to query the database to get all 5 bars. I would prefer to just set an entityKey.
Is this possible?
for example:
Foo myFoo = new Foo();
Foo.Name = "Hello"
Foo.Bars = // now I would like to just populate a set of EntityKeys for the EntityCollection
Just attach a stub entity:
var bar = new Bar { Id = barId };
Context.AttachTo("Bars", bar);
myFoo.Bars.Add(bar);
Context.SaveChanges();
No DB query there for bar
.
Important: You must attach bar
before adding the relationship to myFoo
, so that the EF knows you're not trying to insert a new Bar
.
这篇关于EntityCollections设置一个EntityReference.EntityKey来填充集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!