Entity Framework self referencing [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have the entities User and BlockedUser. A User has many BlockedUsers. BlockedUsers is a list of Users that are blocked by the User. How can I implemented these classes using Code First? After the database is created. There is no trace of the BlockedUser in the User table.
public class User : IdentityUse
{
public List<User> BlockedUser;
}

I just ef database first so I first created tables like below.you have many to many relationship.
create table User
(
ID int primary key,
Name nvarchar(100),
)
create table BlockedUsers
(
UserID int,
BlockUserID int,
primary key( UserID, BlockUserID),
foreign key ( UserID) references User(ID),
foreign key ( BlockUserID) references User(ID),
)
YOU CAN create table then create model from that

There won't be a trace of BlockedUser in the User table. It will create a BlockedUser table which will have a foreign key back to the User table. So you might have five rows in the BlockedUser table eventually, all referring back to User with ID 1. When you navigate to BlockedUsers via the List property on User, EF will load the BlockedUsers for you.
You should create the List property with the virtual keyword if you want lazy-loading of BlockedUsers.

Related

How to design a many to many relation across multiple tables [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am creating a web app using ef core, and I have three entites, Student, Teacher, and Project. Now each of these tables needs to have multiple Links (Id & Url columns here) that point somewhere on the web. What is the best way to design this relation.
I've thought adding a StudentId, TeacherId, and ProjectId nullable column to the Links table. I've thought of creating a Links table for each entity. I've also thought of using a discriminator column, but still, it doesn't feel right.
How do I properly design this?
Note: The Student and Teacher table have a one to one relation with a user table, so I could just put the foreign key there, but the Project table is unrelated to these.
Based on the comments the link, you have a set of links for some or each of your entities in question.
But these are attributes, even a link array is an attribute of those entities, as a link is not an entity on its own. Unfortunately, 1NF requires each attribute to have a unique value. But let's be serious: is this an unbreakable constraint? Of course not. Some RDBMS do have array columns. SQL Server does not. But it has XML and JSON column types. EF has some other tools to provide the same.
Actually it depends on what do you want to do with those links: are they somehow processed with SQL or not. If only the upper tiers are handling them - and I suppose they are processed on the visualization tier - from the database point of view the link array is a single attribute.
If those links are passing trough your business logic, and they are only used during rendering, you can simply store them as comma-separated lists, or JSONS text in a nvarchar with a string property counterpart in the EF entity class, and split/parse them only on the visualization tier. That would make the less concentrated effort.
If your business logic needs them separately, EF does have support for complex types. You can still store your arrays as JSON text in a nvarchar column and serialize-deserialize them transparently. Check here: https://www.codeproject.com/Articles/1166099/Entity-Framework-Storing-complex-properties-as-JSO even better solution here: https://entityframework.net/knowledge-base/14779740/can-i-embed-an-object-in-an-ef-entity--serialize-on-save--deserialize-on-access--
SPARSE is still there of course, if applicable.
You can use below three mapping tables. Which are maintain your many to many relationship.
Like: 4 to 5 students working on same link.
2 teachers handling same link.
create table student_link_mapping(slmid int identity(1,1), studentid int not null, linkid int not null)
create table teacher_link_mapping(tlmid int identity(1,1), teacherid int not null, linkid int not null)
create table project_link_mapping(plmid int identity(1,1), projectid int not null, linkid int not null)

How to write query to insert data in two tables having foreign key relations in SQL Server? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have two tables in SQL Server as:
MobileMaster (MobileId int (PK), MobileName varchar)
and
MobileDetails (MobDetailId int (PK), MobileId int (FK), Color char, Ram int, Camera float)
And I have a Windows form with textboxes for Mobile Name, RAM, Camera, Color.
Now I want to insert the data inserted by user, in these tables in such a way that entry should be done in both tables keeping in mind that MobileID from MobileDetails is a foreign key referencing MobileID in the MobileMaster table, and MobileID and MobDetailsID are kept as Identity (auto-increment).
So help me to do this using SQL Server as database and .net as application development platform. I am not able to write query for it which will be executed when user clicks on the Save button.

Structure for shop (e-commerce) database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I write my project and I will give to user to add his store to my system and then user will can to add shopItem to his store.
My question is:
Maybe already exist shop database for Category-Subcategory.
I mean - When user will add for example boots - he need choose
Category Clothes -> then choose Subcategory Shoes.
So i need database with all category-subcategory for MS SQL - or other database (maximum I will write script for transfer data to MS SQL)
Thank you.
Regards,
David.
You are talking about a hierarchical relationship. Typically you would have a Category database which has a primary key (say, CategoryID). If a subcategory can only belong to single category, then you would have another field in your Category table (maybe called ParentCategoryID). For root-level categories, this field would be NULL. For a subcategory, ParentCategoryID would contain the CategoryID of whatever category is the parent (pretty logical).
If a subcategory can exist under multiple categories, then you need a mapping table that contains the CategoryID and ParentCategory ID.
If you need a subcategory also to be allowed to be a root category, then your mapping table has to take this into account (maybe by allowing NULLs in the ParentCategoryID table)
MSDN has a tutorial on hierarchical data structures here: https://msdn.microsoft.com/en-us/library/bb677213.aspx

how to add instantly made rows key to another table.? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table named product where there is a field named id its the primary key here. I need to put this key to my another table pro_size_tbl as below
pro_size_tbl
==================
id
productid
sizeid
the matter is when the user adds a product the sizes of the products are saved in session and I am trying to save them in pro_size_tbl by the product key what I just made.
The thing I am doing right now is I am adding the product in the product table and in the next line I am retrieving the max id of the product table and using it as the key for the pro_size_tbl.
My question is is there any better way to do this thing?
This is simple.You can use LAST_INSERT_ID().See this sample:
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
There is a function LAST_INSERT_ID() which can give you the desired value.
But you need to use right below the insert statement.
owh. from what i understood, u need to put auto generated mysql id to another table?
if so, here is the real deal
$sql_tbl1 = "insert here for first table";
$result_tbl1 = mysql_query($sql_tbl1) or die(mysql_error());
$id = mysql_insert_id();
$sql_tbl2 = "insert here value ($id, 'bla', 'bla')";
mysql_query(sql_tbl2);
LAST_INSERT_ID() function is ok but it is doing the same work as i am doing taking the maximum value.i guess more or less both way are the same.

How can I make many to one relations in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm writing an exam project. A summary of events is as follows; manager opens an exam and schools for the exam consists of a chairman and two members will establish a commission. Committee members will be selected from the staff table.
In relational database design, you need to use primary keys and foreign keys to relate tables to each other. A primary key is a unique identifier for a table and foreign keys are references to primary keys in other tables. Then you perform joins on your tables to get back the related data you want.
For your case, you need some sort of relation between the two tables that you listed and then you can create a many to one relationship. A many to one relationship consists of many records in one table relating to one record in another table.
For example:
tblCommittee
CommitteeId
Name
DateCreated
Person1Id
Person2Id
tblPerson
PersonId
Name
Sample data:
Person
PersonId Name
1 Billy
2 Joe
3 Bob
4 Jeff
Committee
CommitteeId Name DateCreated Person1Id Person2Id
1 Committee one 1/7/2014 1 2
2 Committee two 12/3/2013 4 3

Categories