代码中的多对多关系第一个模型 - ASP.NET mvc5 [英] Many to many relationship in code first model - ASP.NET mvc5

查看:54
本文介绍了代码中的多对多关系第一个模型 - ASP.NET mvc5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我正在处理代码优先数据库,我在Model文件夹中创建了自己的模型,我不使用实体。我有一个名为Event的模型类,由以下字段组成:EVENTID(STRING)DATE DESCRIPTION IMAGE。我现在要做的是,创建另一个名为Home的类,其中包含类似HOMEID(STRING)DESCRIPTION LINK的列,并且此表类Home必须与类Event有多对多的关系.i从此链接阅读本教程https ://www.infragistics.com/community/blogs/b/dhananjay_kumar/posts/how-to-create-relationships-between-entities-in-the-entity-framework-code-first-approach,但有些事情并不清楚。在自己的列中,我希望将EventID作为导航字段添加为Home类中的外键,以便它将导航到Event类的字段ID。让我假装我已经在两个表的数据库中插入了一行:

Hi all!im working on code first database , i created my own model in Model folder and im not working with entities . I have a model class that is named Event and consists of the fields :EVENTID(STRING) DATE DESCRIPTION IMAGE . What am i trying to do now is , to create another class named Home with columns like HOMEID(STRING) DESCRIPTION LINK , and this table class Home must have a many to many relationship with the class Event .i read this tutorial from this link https://www.infragistics.com/community/blogs/b/dhananjay_kumar/posts/how-to-create-relationships-between-entities-in-the-entity-framework-code-first-approach , but something isnt clear . Insted of its own columns , i want to add EventID as navigation field ,as foreign key in Home class , so that it will navigate to field ID of Event class. Lets pretend i have a row inserted already in database in two of the tables :

Event:
EVENTID:TEST

DATE:01-01-2001

DESCRIPTION:TEST

IMAGE:TEST

_______Home:_________________
HOMEID:TEST1

DESCRIPTION:TEST1

LINK:TEST1



现在,当我使用创建视图添加Home对象时,能够做某事像这样:


So now , when i use create view to add a Home object , be able to do something like this :

Create Home View fields:

HOMEID : test2

DESCRIPTION:test2

LINK : test2

EVENTID :(HERE WE TYPE THE ID OF EVENT OBJECT THAT EXIST IN DATABASE ALREADY)  TEST



然后我渲染这个创建的Home对象的详细信息视图,其id为'test2',能够看到自己的字段和事件表相关字段:

DETAILS VIEW:


Then when i render the Details view of this created Home object with id 'test2' , be able to see something its own fields and Event table related fields :
DETAILS VIEW :

HOMEID : test2

DESCRIPTION:test2

LINK : test2

EVENTID:TEST

DATE:01-01-2001

DESCRIPTION:TEST

IMAGE:TEST





i APPRICIATE每次帮助。提前很多。



我的尝试:



------------ --------------------------------------



i APPRICIATE EVERY HELP.THANKS A LOT IN ADVANCE.

What I have tried:

--------------------------------------------------

推荐答案

创建多对多连接的标准方法是创建所谓的表。

根据您在上面提供的内容(表:事件&表:主页)您将创建第三个表 - 通常名为 Event__Home ,其中包含以下列;

a)主键

b)外键到事件表

c)Home表的外键



当你在Event&添加新的引用时主页在Event__Home表中创建一个新记录,其中包含Event Id&家庭ID。

活动模型&然后应该更新Home以包含来自Event__Home表的集合。

MVC将允许您遍历这些以显示相关表中的其他值。



以下文章将为您提供有关多对多连接等的其他信息;

MSDN:操作方法:多对多关系 [ ^ ]



亲切的问候
The standard method of creating a many-to-many join is to create what is known as a junction table.
Based on what you have provided above (Table: Event & Table: Home) you would create a third table - normally named Event__Home that consists of the following columns;
a) Primary Key
b) Foreign Key to Event table
c) Foreign Key to the Home table

When you add a new reference between Event & Home a new record is created in the Event__Home table that comprises of the Event Id & the Home Id.
The Models for Event & Home should be then updated to include collections from the Event__Home table.
MVC will allow you to traverse these to display additional values from the relevant table.

The below article will give you additional information on Many-to-Many joins etc;
MSDN: How-To: Many to Many Relationships[^]

Kind Regards


这篇关于代码中的多对多关系第一个模型 - ASP.NET mvc5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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