The table sits on Azure SQL Database, and was created with
CREATE TABLE [dbo].[ADVUser]
(
[Id] INT IDENTITY(1,1),
[UserId] UNIQUEIDENTIFIER NOT NULL,
[CRMTItemId] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
FOREIGN KEY ([CRMTItemId]) REFERENCES [dbo].[CRMTItem] ([Id])
);
and I'm inserting with
using (var db = new Entities())
{
try
{
db.ADVUsers.Add(new ADVUser()
{
CRMTItemId = "000001",
UserId = new Guid("e1ef8e25-1624-46f0-9336-1674f1cffaeb")
});
db.SaveChanges();
}
catch (Exception e)
{
throw;
}
}
Where CRMItem
with id 000001
already exists.
I also tried
using (var db = new ADVWKSPEntities())
{
var item = db.CRMTItems.First();
try
{
item.ADVUsers.Add(new ADVUser()
{
UserId = new Guid("e1ef8e25-1624-46f0-9336-1674f1cffaeb")
});
db.SaveChanges();
}
catch (Exception e)
{
throw;
}
}
What is going on?
Update
I am using database first method, and this is the model that was generated for ADVUser
:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace ADVWKSP.Models
{
using System;
using System.Collections.Generic;
public partial class ADVUser
{
public int Id { get; set; }
public System.Guid UserId { get; set; }
public string CRMTItemId { get; set; }
public virtual CRMTItem CRMTItem { get; set; }
}
}