EntityCollections设置一个EntityReference.EntityKey来填充集合 [英] EntityCollections setting an EntityReference.EntityKey to populate the Collection
问题描述
对实体框架有麻烦我一直使用EntityKey填充EntityReferences,以免在添加实体引用时无需查询数据库。现在我想为协会(EntityCollection)做同样的事情,但我不知道从哪里开始。以下是我正在尝试做的细节
表
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])
参考[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])
参考[dbo]。[Foo]([FooId])
ALTER TABLE [dbo]。 [FooBar] CHECK CONSTRAINT [FK_FooBar_Foo]
现在w母鸡我将这个映射到实体框架我得到2个实体(Foo和Bar)与多对多关系。
我想要做的是添加一个新的Foo实体5条关联
我有Bars的ID,但是我不想查询数据库以获取所有5个条。
这是可能吗?
例如:
Foo myFoo = new Foo();
Foo.Name =Hello
Foo.Bars = //现在我只想为EntityCollection添加一组EntityKey
只需附加 stub entity :
var bar = new Bar {Id = barId};
Context.AttachTo(Bars,bar);
myFoo.Bars.Add(bar);
Context.SaveChanges();
没有DB查询 bar
/ p>
重要提示:您必须在添加之前附加 bar
关系到 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屋!