本文介绍了查询协会 - FK设置错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有真正费心去发帖,因为互联网上总是有很多资源,但我只是不明白我想如何在我的数据库中映射FK关系以正常使用EF。我是CS的学生,我的学校在那里教java - 但我总是发现自己对微软技术感到惊讶。所以我们走了。

我有一个存储文化的数据库(例如博士办公室)。让我困惑的相关表格:

活动
GroupNames(可能只是名称组)
GroupActivity
用户

活动table由两列组成:ID和Name
GroupName表由两列组成:ID和Name
GroupActivity表由两列组成:GroupID和ActivityID(它链接上面的两个表) - 两列PK用户表用户表有很多列,其中一列是GroupID

FK的设置如下:
FK-Users> GroupID上的PK-GroupName(我昨晚设置的那个然后从db更新我的实体模型)
FK-GroupActivity>对活性ID的PK活性作为FK-Group活性> GroupID上的PK-GroupName

EF没有添加GroupActivity表,因为它很聪明 - 但是当试图访问用户活动时 - 我遇到了问题。

所以这里是我设置了几个测试查询 - 我的问题是为什么我必须有一个嵌套循环来访问Activity.FullName属性?我应该设置不同的东西吗?

 CultureEntities db = new CultureEntities(); 

var test =来自db.GroupNames中的g
其中g.ID == General.LoggedInGroupID
select g.Activities;

var test2 =来自u的db.Users
其中u.UserID == General.LoggedInUserID
select u.GroupName.Activities;


foreach(测试中的var tObj)
{
foreach(tObj中的var a)
Console.WriteLine(a.FullName);
}

foreach(test2中的var tObj)
{
foreach(tbb中的var b)
Console.WriteLine(b.FullName);
}
解决方案

I never really bother posting a question because there are always great resources on the internet, but I'm just not understanding I guess how to map the FK relationships in my database to work properly with EF. Im a student of CS, where my school teaches java - but I always find myself amazed by microsoft technologies. So here we go.

I have a database for storing cultures (for a dr's office for instance). The pertinent tables that have me confused:

Activity
GroupNames (probably should just be name Group)
GroupActivity
User

The Activity table consists of two columns: ID and Name
The GroupName table consists of two columns: ID and Name
The GroupActivity table consists of two columns: GroupID and ActivityID (it links the two above tables) - both columns are PK's
The User table has many columns, one being GroupID

FK's are setup like this:
FK-Users > PK-GroupName on GroupID (the one I setup last night then updated my entity model from db)
FK-GroupActivity > PK-Activity on ActivityID
FK-GroupActivity > PK-GroupName on GroupID

EF didnt add the GroupActivity table because its smart like that - but when trying to get to the Users activites - im having problems.

So here are a couple of test queries I setup - my question is why do i have to have a nested loop to access the Activity.FullName property? Should I set something up differently?

CultureEntities db = new CultureEntities();

            var test = from g in db.GroupNames
                       where g.ID == General.LoggedInGroupID
                       select g.Activities;

            var test2 = from u in db.Users
                        where u.UserID == General.LoggedInUserID
                        select u.GroupName.Activities;


            foreach (var tObj in test)
            {
                foreach (var a in tObj)
                    Console.WriteLine(a.FullName);
            }

            foreach (var tObj in test2)
            {
                foreach (var b in tObj)
                    Console.WriteLine(b.FullName);
            }
解决方案


这篇关于查询协会 - FK设置错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 08:52