I know I am missing something. Its probably obvious but I cant see it and I cant find an answer on Google I understand.
I am getting this loverly error message when I run my initialiser:
System.Data.SqlClient.SqlException
Cannot insert duplicate key row in object 'dbo.Site' with unique index 'IX_Site_Clientid'. The duplicate key value is (68).
The statement has been terminated.
Which would be nice if it made sense. Why it doesn't make sense is it is telling me the Site Table has a unique index for Client linking ID. This is wrong and kind of completely misses the design. However until I introduced the populating in the Initialiser it was working fine, I could have any number of sites for the client which is what I always intended.
Models:
Client:
public class Client
{
public int Clientid { get; set; }
public string ShentonAcc { get; set; }
[Required]
[StringLength(120, MinimumLength = 3)]
[Display(Name="Business Name")]
public string Name { get; set; }
[Required]
[StringLength(120, MinimumLength = 3)]
[Display(Name = "Address")]
public string Address { get; set; }
[Required]
[StringLength(120, MinimumLength = 3)]
public string Suburb { get; set; }
[Required]
[StringLength(10, MinimumLength = 3)]
[Display (Name="Post Code")]
public string PostCode { get; set; }
[Required]
[Display(Name = "Account Email")]
public string AccountEmail { get; set; }
[Required]
[Display(Name = "Account Contact")]
public string AccountContact { get; set; }
[Required]
[StringLength(20, MinimumLength = 3)]
[Display(Name = "Office Phone")]
public string OfficePhone { get; set; }
public ICollection<Site> Site { get; set; }
public ICollection<SiteInduction> SiteInduction { get; set; }
public ICollection<ClientContact> ClientContact { get; set; }
}Site:
public class Site
{
public int SiteID { get; set; }
public int Clientid { get; set; }
[Required]
[StringLength(80, MinimumLength = 3)]
[Display(Name = "Site")]
public string SiteName { get; set; }
[Required]
[Display(Name = "Facility Type")]
public int SiteTypeID { get; set; }
[Required]
[StringLength(120, MinimumLength = 3)]
[Display(Name = "Address")]
public string SiteAddress { get; set; }
[Required]
[StringLength(80, MinimumLength = 3)]
[Display(Name = "Suburb")]
public string SiteSuburb { get; set; }
[Required]
[StringLength(8, MinimumLength = 3)]
[Display(Name = "Post Code")]
public string SitePostCode { get; set; }
public SiteType SiteType { get; set; }
public Client Client { get; set; }
public ICollection<WaterBody> WaterBodys { get; set; }
//public WaterBody Waterbody { get; set; }
public ICollection<Job> Job { get; set; }
public ICollection<QuoteRequest> QuoteRequest { get; set; }
public ICollection<SiteAccess> SiteAccess { get; set; }
public ICollection<SiteTask> SiteTask { get; set; }
//public ICollection<SiteInduction> SiteInduction { get; set; }
}SQL Index from Site table:
CREATE UNIQUE NONCLUSTERED INDEX [IX_Site_Clientid]
ON [dbo].[Site]([Clientid] ASC) WHERE ([Clientid] IS NOT NULL);I cant see any reason this is unique. It certainly shouldn't be.
As it happens I have an old version of the database still in SQL and this index wasn't previously unique. But I made no changes to this code for about the last 5 revisions. The only change is I am now populating from the initialiser. As the initialiser is quite a lot, I wont load it all up. I will put a small virgin in here to show it in case this is creating the error.
Initialiser
var client = new Client[]
{
new Client { ShentonAcc= "AWB", Name ="AWB Building Co" , Address="4/20 Milford Street" , Suburb="East Victoria Park" , PostCode="6101", AccountEmail="vaughan@awbco.com.au" , AccountContact="cccc" , OfficePhone=" gfusjryur6 " },
new Client { ShentonAcc= "BGC", Name ="BGC Construction" , Address="22 Mount St" , Suburb="Perth" , PostCode="6000", AccountEmail="martinh@bgcconstruction.com" , AccountContact="xbvg" , OfficePhone="999999999" }
};
foreach (Client c in client)
{
context.Clients.Add(c);
}
context.SaveChanges();
var site = new Site[]
{
new Site { Clientid= 1, SiteName ="Hotel" , SiteAddress="500 Hay Street" , SiteSuburb="Subiaco" , SitePostCode="6008", SiteTypeID=26 },
new Site { Clientid= 2, SiteName ="Iluka Aquatic Centre (Gingin)" , SiteAddress="New Street" , SiteSuburb="Gingin " , SitePostCode="6503", SiteTypeID=1 },
new Site { Clientid= 3, SiteName ="Dalwallinu Aquatic Centre" , SiteAddress="Myers Street" , SiteSuburb="Dalwallinu" , SitePostCode="6609", SiteTypeID=1 }
};
foreach (Site s in site)
{
context.Sites.Add(s);
}
context.SaveChanges(); Questions:
What is causing the error? Cannot insert duplicate key row in object
Why has this index changed to a Unique index for no apparent reason?
How do I prevent this issue in future?