Model. В таблицах указал
CONSTRAINT [FK_Profiles_Infos] FOREIGN KEY ([InfoId])
REFERENCES [dbo].[Infos] ([Id]) ON DELETE SET NULL
CONSTRAINT [FK_Сars_Infos] FOREIGN KEY ([InfoId])
REFERENCES [dbo].[Infos] ([Id]) ON DELETE SET NULL
namespace Meditid.Models
{
public class Info
{
[Key]
public int Id { get; set; }
public virtual Profile Profile { get; set; }
public virtual Collection<Car> Car { get; set; }
}
public class Profile
{
public int Id { get; set; }
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public int InfoId { get; set; }
public virtual Info info { get; set; }
}
public class Car
{
public int Id { get; set; }
public string CarName { get; set; }
public string CarNumber { get; set; }
public int InfoId { get; set; }
public virtual Info info { get; set; }
}
public class SContext : DbContext
{
public DbSet<Profile> Profiles { get; set; }
public DbSet<Car> Cars { get; set; }
public DbSet<Info> Infos { get; set; }
}
}
Функция формирования json'a из 3 таблиц
public string Lowx()
{
var query = db.Infos
.Include(x => x.Profile)
.Include(x => x.Car)
.ToList();
return JsonConvert.SerializeObject(query.ToList());
}
Выдает исключение:
System.InvalidOperationException: "Unable to determine the principal end of an association between the types 'Meditid.Models.Profile' and 'Meditid.Models.Info'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."
HasKey
это другое. это устанавливаетPrimaryKey
. ПопробуйтеWithRequered
илиWithOptional
в зависимости от того, что вам нужно.. Аналогично и сHas