DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Issue inserting data using IRepository to insert data with Guid(Uniqueidentifier) as the primary key

Sort:
You are not authorized to post a reply.





New Around Here





    I have a table with Guid(i.e. Uniqueidentifier) as the primary key. When I try to insert a new row with IRepository I am getting the following error, even though I am filling in FolderUid , which is the primary key, property with a new Guid:

    Error message:

    Cannot insert the value NULL into column 'FolderUid', table 'xxx'; column does not allow nulls. INSERT fails.

    My Code:

    using (IDataContext db = DataContext.Instance()) {
                    IRepository<FolderData> fileRepo = db.GetRepository<FolderData>();
                    folderData.FolderUid= Guid.NewGuid();
                    fileRepo.Insert(folderData);
                }

    How can I fix the issue?






    Veteran Member





      Hello Ana: 

      It sounds like you may be using the DNN DAL 2 feature that utilizes PetaPoco under the covers.  I don't think that it supports using a GUID as a primary key.  However, I've gotten around this in the past by having two key fields.  First is a traditional int (PK), and then another that is used by the code as the PK but it's a GUID.  It takes a few more minutes to wire up in your controller class(es), but it's a one and done thing.  

      If you don't want to use this workaround, you're not limited to the DNN DAL options.  You can use any other ORM that you wish.  If it can work in .NET, it can work in DNN.  (That is, assuming it's one that newer versions of .NET can also run.)  This includes the most popular option that I see, Entity Framework, but we also use Dapper a lot, for example.  






      Advanced Member





        If you happen to look into Entity Framework (EF), there are many flavors. For use with DNN, you'd want to use EF6.

        David Poindexter
        Corporate Website  |  Facebook Page  |  Twitter Page

        nvisionative
        Creator of:





        Advanced Member





          You can use EFCore but only up to v3.1 as they dropped .NetStrandard compatibility after that version. There are pros and cons for EF6 vs EFCore but so far I have used EF6 in most DNN extensions after balancing the pros and cons.

          You can read more about all the supported features to decide here https://learn.microsoft.c...s/ef/efcore-and-ef6/

          And if you need both EF6 and EFCore in the same project that is possible too :)
          https://learn.microsoft.c...and-ef6/side-by-side
          You are not authorized to post a reply.

          These Forums are dedicated to the discussion of DNN Platform.

          For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

          1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
          2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
          3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
          4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
          5. No Flaming or Trolling.
          6. No Profanity, Racism, or Prejudice.
          7. Site Moderators have the final word on approving / removing a thread or post or comment.
          8. English language posting only, please.

          Would you like to help us?

          Awesome! Simply post in the forums using the link below and we'll get you started.

          Get Involved