I am creating a project using asp.net core web api and cosmos db. I generate the id as guid value, I auto generate the id.But it create duplicate value.
work.cs file:
public class work
{
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
public List<Industy> Industy { get; set; }
public work()
{
if (Id == null)
{
Id = Guid.NewGuid();
}
else
{
Id = Id;
}
}
}
Industry.cs file:
{
[JsonProperty("Id")]
public Guid Id { get; set; }
[JsonProperty("IdustryId")]
public int IdustryId { get; set; }
[JsonProperty("IdustryName")]
public string IdustryName { get; set; }
public Industy()
{
if (Id == null)
{
Id = Guid.NewGuid();
}
else
{
Id = Id;
}
}
}
output:
{
"id": "00000000-0000-0000-0000-000000000000",
"Name": "string",
"industy": {
"id": "00000000-0000-0000-0000-000000000000",
"IdustryId": 0,
}
}
If i enter more than one value without id it show me the error ,id is already exist. please help me to fix it.
Mark
public Guid Id { get; set; }
as nullable in both models:public Guid? Id { get; set; }
Guid is a struct and value type. This means you have to compare with its default value instead of null or mark it as nullable.