实体框架6从列表中保存 [英] Entity framework 6 save from list

查看:70
本文介绍了实体框架6从列表中保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我参与了我的第一个EF6项目,我有几个问题



项目概述

物业(大楼)有房间

房间有标准或评级列表

每个标准都有一组价值



物业(大楼)A有

房间1

每个房间都有很多价值

价值0-1

价值B 2-5 ......等



房间2

每个房间有一堆价值

价值a 1-3

价值B 0-5 ...等





物业(大楼)B有

房间1

每个房间都有一堆价值

价值0-1

价值B 4-9

价值c 0-1 ......等



房间2

每个房间都有很多价值

Val你是1-3

价值B 0-4 ...等

...





因此,一个属性(建筑物)本身就有一个房间列表,每个房间都有自己的价值

因此,数据库设计是

物业1 ... N客房

客房1 ... N价值



这些价值可根据需要修改



我使用EF6来处理数据库,而Caliburn.Micro BindableCollection

使用DbSet从数据库中提取数据

现在,困境是保存Rooms及其值而不重复,我尝试使用循环更新dbSet,但这只会导致数据重复。

我无法找到一种方法从数据中清空DbSet而不回复意大利面条代码,我试图避免使用



我的问题是如何将数据保存到数据库中没有

1-复制

2-意大利面条和非常难看的代码

3-删除我正在使用的框架



提前感谢



我尝试了什么:



空DBContext及其DbSet

将ICollection转换为DbSet

成功保存,但重复数据

Hello
I am on my first EF6 project and I have couple of questions

Project outlines
Property (Building) have Rooms
A Room have list of criteria or rating
Each criteria own a set of values
i.e.
Property (Building) A have
Room 1
Each room have bunch of values
Value a 0-1
Value B 2-5 ... etc.

Room 2
Each room have bunch of values
Value a 1-3
Value B 0-5 ... etc.


Property (Building) B have
Room 1
Each room have bunch of values
Value a 0-1
Value B 4-9
Value c 0-1 ... etc.

Room 2
Each room have bunch of values
Value a 1-3
Value B 0-4 ... etc.
...


Therefore, by nature a property (Building) have a list of rooms, each room have its own values
Therefore, the database design is
Property 1...N Rooms
Rooms 1...N Values

These values can be modified on desire

I am using EF6 to handle the DB, and Caliburn.Micro BindableCollection
Pulling the data from the DB using DbSet
Now, the dilemma is to save the Rooms and its values without duplication, I tried updating the dbSet using the loops but that only caused data duplication.
I could not find a way to empty the DbSet from its data without reverting to spaghetti code, which I’m trying to avoid

My Question is How to save the data to the DB without
1- Duplication
2- Spaghetti and very ugly code
3- Dropping the frameworks I am using

thanks in advance

What I have tried:

Empty DBContext and its DbSet
Convert ICollection to DbSet
Saved successfully but with Duplicated data

推荐答案

正确

首先抱歉粗糙的英语,我现在很乱了

解决方案是如下:



循环通过现有的主要类别(物业(建筑物))

for Property.Count

属性[i] .Rooms = inProperty [i] .Rooms



等等其他嵌套列表



PropertyDBContext.SaveChanges();



如果你们有更好的解决方案,我将永远欢迎c& c
Right
first of all sorry for the rough English, I am a mess right now
the solution is as follows:

Loop through the existing Main categories (Property(Building))
for Property.Count
Property[i].Rooms = inProperty[i].Rooms

So on and so forth for the other nested lists

PropertyDBContext.SaveChanges();

if you guys have a better solution i will always welcome c&c


这篇关于实体框架6从列表中保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆