EF 6 - Creating entity with n-n
I want to add "Tags" to this but there is no table for many to many in new versions of EF.
48 Replies
public virtual ICollection<Tag> Tags { get; set; }
I have this in my entity.Collection of A in B, collection of B in A
All that's needed
And get rid of any and all
virtual
in your modelsthat's database first, i will delete the virtuals in dtos
if there is one
but i didn't understand a and b thing
Example model names
let me process it for a minute 😄
First result on Google:
The second most common type of relationship is known as a Many To Many relationship. The following diagram shows how this appears in a database diagram. Each book can belong to many categories and each category can contain many books. This type of relationship is managed in a database through the use of a join table (also known among other things as a bridging, junction or linking table). This type of relationship is defined in code by the inclusion of collection properties in each of the entities:
yeah i already knew that though. But how do I add the Tags, I tried filling this collection but it gave an error
also i use mapper, maybe there is something about it too
oh
You need the actual model intances too, not mapped DTOs
but i am using dto, is there an other way
No
let me try this first
thanks btw
You can see the flow like this:
1) Request comes from the client to get data: Model -> DTO
2) Client sends data that results in creating or updating something: DTO -> Model
2) yes i am using mapper for that
db.Posts.Add(mapper.Map<Post>(newpost));
db.Posts.Tags
Doesn't exists i guessYou don't have N-N set up, then
public virtual ICollection<Tag> Tags { get; set; }
it's there
Ah
You need a specific post
Not all the posts
i get the tags, i just can't create them in n n table
Of course a
DbSet
of Post
s won't contain a property Tags
so i was thinking i could get the newly created post's id and then add the tags into the n n table. But simply there is no table in ef core 6 😄
let me check what you wrote
You can add it to the newly-created post tho..?
I have Post table, Tags table and Post_Tag table.
post.Tags.Add(new Tag { ... });
I don't think that would workunless the "tags" here is the n n table here?
oh i see
but i tried this and got an error
Nothing here is the join table
I think you need to get away from that thinking in relations in databases for a moment
There's zero join tables C#-side
and switch to classes and objects
okay okay i just saw that part. I am just saying i tried this filling the property thing. Catch was triggered, but let me check the error first
should i create ICollection or List?
as property
List
You cannot instantiate interfaces
Ah, property?
ICollection
, List
, either's finealright
is giving id only fine? Tags has an id and body.
Ideally, you'd fetch the tags by ID from the database, but you can use an instance of
Tag
with ID alone, I thinkSystem.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64.
Ah, don't return database models from your API
public List<TagDtoWrite_1>? Tags { get; set; }
i am not returning it 😄
Maybe because of the
virtual
?
It enables lazy loading
Dunnothis error is completely irrelevant lol
There's a cycle somewhere in what you return
oh maybe it was a different error, i saw it in the terminal. It says this;
Cannot insert explicit value for identity column in table 'Tag' when IDENTITY_INSERT is set to OFF.
i am not trying to create a tag am i 😄
Ah, when you're adding tags to the post, you do it with
new Tag { Id = id }
EF takes it as an attempt at creating a tagi used swagger for that
"tags": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
So you either have to track those tags, to tell EF those are shims for existing ones, or you have to fetch the tags from the db
This would be the safest way
Or... you can create an entity that represents the join table and configure it
But I hear you use DB-first, so no clue how to do that
I refuse to touch DB-first with a ten-foot pole
it's so fun so far, why tho
code first sounds messy
It's the opposite, in my experience
You write the code how you want it, define all the relationships as you need them, and never worry about the database
At any and all times you work with C#
There's no
virtual
s being generated, no BeanDto_5367
si see, maybe i learn code first when this is over
foreach (var t in request.Tags)
{
newpost.Tags.Add(t);
}
This add method gives null error. Maybe I can't get the Tags from swagger at all 😄 I will return in 5 minutesNeed to intialize the tags collection
Or you can just do
newpost.Tags = request.Tags
yup it was already like that, i tried your way to see if there will be a difference and I noticed the null error
while it's nullable, foreach gave an error like that
welp, i can return the data and it gives me all the tags. I don't know why it says it's null
@Angius I remembered your advice about creating an entity instead of the dto. I don't know why I insisted about creating dto and mapping it. I am already getting the data as dto so there is no security risk or anything 😄 It worked, thanks.