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 fileRepo = db.GetRepository(); folderData.FolderUid= Guid.NewGuid(); fileRepo.Insert(folderData); }
How can I fix the issue?
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 <code>GUID</code> as a primary key. However, I've gotten around this in the past by having two key fields. First is a traditional <code>int (PK)</code>, and then another that is used by the code as the <code>PK</code> but it's a <code>GUID</code>. 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.
If you happen to look into Entity Framework (EF), there are many flavors. For use with DNN, you'd want to use EF6.
These Forums are for the discussion of the open source CMS DNN platform and ecosystem.
For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:
Awesome! Simply post in the forums using the link below and we'll get you started.