Multiple New records - Junction table

I have three tables Clients, Notes and Opportunities related as follows:
Clients (1-many) Notes
Clients (1-many) Opportunities

and a fourth a junction table OppNotes with FKs: ClientID, OpportunityID, NoteID

I want to add new notes from the opportunities page and to do that I need to:
add new Note (with ClientID FK) and at the same time add new OppNotes.

How can I save the new Note, get the NoteID and use that to insert into OppNote?

There is a closed thread and an Angular example on github but I'm doing this in Blazor. Does anyone have an eample or some help. Is it possible throughthe Radzen UI or do i need to do it in VS etc?

thanks

John

The Angular example is the only thing existing at the moment. You can examine it and replicate it in Blazor. The custom code isn't that much and should be easy to port.

Folks,
I have managed to get this to work now, I have one further question:

I am populating an empty datagrid from a dropdown and a button to add new records for an intermediate table. this works but i would like the values in the intermediate table to display the master table.

tblClient<>tblClientTag<>tblTag

I have a custom partial class with the follwing:

   >  List<TblClientTag>lstCT= new List<TblClientTag>();
    public IEnumerable<TblClientTag> AddClientTag(int CID,int TID)
    {

        TblClientTag tblClientTag = new TblClientTag();
        tblClientTag.ClientID = CID;
        tblClientTag.TagID = TID;

        lstCT.Add(tblClientTag);

// Results for the datagrid but it will only display the integer value form tblClientTag
return lstCT;
}

//Save the results to the db
public async TaskSaveClientTag(int CID)
{

        foreach( TblClientTag item in lstCT) {
            item.ClientID = CID;
            dbContext.TblClientTags.Add(item);
        }
       
        //tblClientTags
        dbContext.SaveChanges();

        return true;
    }

The datagrid uses the ClientTag results to display but how can i get the tag string value stored in tblTag to show? ${data.tblTag?.Tag} shows nothing. What am I missing?

thanks

John

What is tblTag? We don't have the full context here.

I have a tblClient and tblTag and a client can have many Tags so I have an intermediate table tblClientTag which stores the ClientID and the TagID as FKs.

When i populate the datagrid from my list it shows the clientID and the TagID but it would be nicer for it to show the TagName which is stored in tblTag. If i was populating this datagrid from the invoke datasource method I could use ${data.tbltag?.Tagname} to get the string value to appear in a column but using the LIst does not give me anything other than the ID column

As far as I see you don't set any tblTag property in your AddClientTag method. Also Radzen never generates lowercase properties for a table. Check if the TblClientTag class even has a tblTag property.

namespace xxxx.Models.xxxx
{
[Table("tblClientTag", Schema = "dbo")]
public partial class TblClientTag
{
[Key]
public int ClientID
{
get;
set;
}
public TblClient TblClient { get; set; }
[Key]
public int TagID
{
get;
set;
}
public TblTag TblTag { get; set; }
}
}

this is the tblClientTag class

and the Tag ID is set by the parameter TID, ClientdID, CID is always zero because i'm adding this before the tblclient is saved then once I've got eh scope__identity value back I change the ClientID in the List using the foreach loop

>     public IEnumerable<TblClientTag> AddClientTag(int CID,int TID)
>     {
> 
>         TblClientTag tblClientTag = new TblClientTag();
>         tblClientTag.ClientID = CID;
>         tblClientTag.TagID = TID;
> 
>         lstCT.Add(tblClientTag);
> ```
> 
> // Results for the datagrid but it will only display the integer value form tblClientTag
> return lstCT;
> }

Nothing is populating the TblTag property still. You need to populate it somehow in the AddClientTag method.

Can you tell me how to do that or at least give me a clue.

thanks

I would just let Entity Framework do it.

lstCT = dbContext.TblClientTags.Include(t => t.TblTag).ToList()

This code should run after dbContext.SaveChanges is executed. You can check this article that explains how Entity Framework loads related entities such as the TblTag.

Atanas,

Because I want to have these records displayed before they are saved to the db your method won't work in this instance. I have got round the problem by creating another list class which holds the string value from tblTag and using this to populate the datagrid whilst keeping the original list class with type tblClientTag so when I submit the form these records are saved to the db.

Seems a slightly cumbersome way of doing it but it works.

Thanks for your help.

namespace EsWlite.Pages
{
public partial class AClientAddComponent
{
[Inject]
dbContext dbContext { get; set; }

    //list for use with dbContect.SaveChanges
    List<TblClientTag>lstCT= new List<TblClientTag>();

    //list for datagrid display
    List<ClientTagName> LCTN = new List<ClientTagName>();

    public IEnumerable<ClientTagName> AddClientTag(int CID,int TID)
    {
        //used to store the new records prior to being saved to db
        TblClientTag tblClientTag = new TblClientTag();
        tblClientTag.ClientID = CID;
        tblClientTag.TagID = TID;
        lstCT.Add(tblClientTag);

        //used to populate the datagrid
        var CTN = new ClientTagName();
        CTN.Tag = TID;
        //get the string value for display
        CTN.TagName = (from c in dbContext.TblTags where c.TagID == TID select (c.Tag)).FirstOrDefault();
        LCTN.Add(CTN);

        return LCTN; 
    }

    public async Task<bool>SaveClientTag(int CID)
    {
        foreach( TblClientTag item in lstCT) {
            item.ClientID = CID;
            dbContext.TblClientTags.Add(item);
        }

        dbContext.SaveChanges();
        return true;
    }
}

public class ClientTagName
{
    public int Tag { get; set; }
    public string TagName { get; set; }

}

}