'Defining multiple Id columns using Marten?
I'm currently using the default Id implementation of Marten for this Object fe
public class DbOBject
{
public string Id { get; set; }
public string Name { get; set; }
public int Type { get; set; }
}
Now I want to define the Id and the Type column als Identifier, is this possible?
Solution 1:[1]
I don't believe there's a fully straightforward way to go about it.
Marten requires one Id field (or a property marked as an identifier).
I'd personally create a new field that is a composite of Id and Type (using concatenation or whatever you find most readable). And then use it as an identity.
If you're happy with implementing Marten interfaces and insist on a cleaner solution, I suggest you read these docs:
https://martendb.io/documents/identity.html#custom-identity-strategies
The links for interfaces and implementations are broken, but here's a working link to Marten's Identity generation:
https://github.com/JasperFx/marten/tree/master/src/Marten/Schema/Identity
This, however, will require you to have a separate key generator just for this specific class, which may actually make your code less readable (given that you should also configure a specific store just for it, in order to use the new custom generator).
Solution 2:[2]
Thanks, I've implemented the first solution
public class DbOBject
{
public string Id { get { return BusinessValue + "_" + Type.ToString(); } }
public string Name { get; set; }
public string BusinessValue{ get; set; }
public int Type { get; set; }
}
The Id was an unique business value that turned out not to be that unique. So I added the busisess value as a property and used the id as a combination of that business value and the type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Phill |
Solution 2 | Werner Kellens |