C# Nhibernate mapping to lookup table - c#

I'm having a difficult time mapping an entity to another entity that is a lookup table.
create table Sources
(
SourceId identity(1,1) primary key not null,
Name [nvarchar](255) NULL,
)
create table Candidates
(
CandidateId int identity(1,1) primary key not null,
SourceId int references Sources(SourceId) NULL,
)
And Enitites:
public class Candidate : Entity
{
public virtual Source Source { get; set; }
}
public class Source : Entity
{
public virtual string Name { get; set; }
}
I'm getting a error:
An association from the table Candidates refers to an unmapped class:
Entities.Source
But I am unsure how to go about the mapping:
public class CandidateMap : IAutoMappingOverride<Candidate>
{
public void Override(AutoMapping<Candidate> mapping)
{
mapping.Map(x => x.Source);
}
}

The problem was I was inheriting from the Entity that was part of NHibernate namespace, instead of the SharpArch.NHibernate namespace. SharpArch does some mapping that the project I was working in was using.

Related

One-to-one becomes one-to-many Entity Framework generating database-first

I'm generating Entity Framework database first (EF Designer from database). I've got two tables in Microsoft SQL Server:
CREATE TABLE dbo.Person
(
Pk_Person_Id INT IDENTITY PRIMARY KEY,
Name VARCHAR(255),
EmailId VARCHAR(255),
);
CREATE TABLE dbo.PassportDetails
(
Pk_Passport_Id INT PRIMARY KEY,
Passport_Number VARCHAR(255),
Fk_Person_Id INT UNIQUE
FOREIGN KEY REFERENCES dbo.Person(Pk_Person_Id)
);
INSERT INTO dbo.Person
VALUES ('Niraj','v.a#emails.com'),
('Vishwanath','v.v#emails.com'),
('Chetan','c.v#emails.com');
GO
INSERT INTO dbo.PassportDetails
VALUES (101, 'C3031R33', 1), (102, 'VRDK5695', 2), (103, 'A4DEK33D', 3);
GO
SELECT * FROM dbo.Person
SELECT * FROM dbo.PassportDetails;
In SQL Server the relations are shown as one-to-one because Fk_Person_Id is set as isUnique = true:
Fk_Person_Id INT UNIQUE
FOREIGN KEY REFERENCES dbo.Person(Pk_Person_Id));
Now in Visual Studio, I add a new ADO.NET Entity Data Model -> EF Designer from database -> select these two tables and leave all checkbox options by default.
Then after generation is over I see this in Visual Studio diagram .edmx:
And the relationship has changed for one-to-many - why? Is this wrong? I don't want a person to have a collection of passports - it's not the logic I'm trying to describe.
And the EF code:
public partial class Person
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Person()
{
this.PassportDetails = new HashSet<PassportDetail>();
}
public int Pk_Person_Id { get; set; }
public string Name { get; set; }
public string EmailId { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PassportDetail> PassportDetails { get; set; }
}
public partial class PassportDetail
{
public int Pk_Passport_Id { get; set; }
public string Passport_Number { get; set; }
public Nullable<int> Fk_Person_Id { get; set; }
public virtual Person Person { get; set; }
}
Context
public partial class LightCRMEntities : DbContext
{
public LightCRMEntities() : base("name=LightCRMEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<PassportDetail> PassportDetails { get; set; }
public virtual DbSet<Person> People { get; set; }
}
Well, in database model editor (.edmx file editor), by clicking on a relationship (arrow between entities daigrams), you can configure its properties in such way, to make it 1-to-1 (it won't change your database!), but then you can generate the code you need to create such database. In result, I got the following:
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table 'PassportDetails'
CREATE TABLE [dbo].[PassportDetails] (
[Pk_Passport_Id] int NOT NULL,
[Passport_Number] varchar(255) NULL
);
GO
-- Creating table 'Person'
CREATE TABLE [dbo].[Person] (
[Pk_Person_Id] int IDENTITY(1,1) NOT NULL,
[Name] varchar(255) NULL,
[EmailId] varchar(255) NULL
);
GO
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [Pk_Passport_Id] in table 'PassportDetails'
ALTER TABLE [dbo].[PassportDetails]
ADD CONSTRAINT [PK_PassportDetails]
PRIMARY KEY CLUSTERED ([Pk_Passport_Id] ASC);
GO
-- Creating primary key on [Pk_Person_Id] in table 'Person'
ALTER TABLE [dbo].[Person]
ADD CONSTRAINT [PK_Person]
PRIMARY KEY CLUSTERED ([Pk_Person_Id] ASC);
GO
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [Pk_Passport_Id] in table 'PassportDetails'
ALTER TABLE [dbo].[PassportDetails]
ADD CONSTRAINT [FK__PassportD__Pk_Pa__5BE2A6F2]
FOREIGN KEY ([Pk_Passport_Id])
REFERENCES [dbo].[Person]
([Pk_Person_Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

why code first want to create index column?

I have two table like below:
[Table("MyFlashCard")]
public partial class MyFlashCard
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public MyFlashCard()
{
MyFlashCardPics = new HashSet<MyFlashCardPic>();
}
public int Id { get; set; }
public int? FaslID { get; set; }
public virtual FasleManJdl FasleManJdl { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MyFlashCardPic> MyFlashCardPics { get; set; }
}
[Table("MyFlashCardPic")]
public partial class MyFlashCardPic
{
public int Id { get; set; }
[ForeignKey("MyFlashCard")]
public int MyFlashCardId { get; set; }
public virtual MyFlashCard MyFlashCard { get; set; }
}
and a ModelBuilder:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyFlashCard>()
.HasMany(e => e.MyFlashCardPics)
.WithRequired(e => e.MyFlashCard)
.HasForeignKey(e => e.MyFlashCardId)
.WillCascadeOnDelete();
}
and when I add migration it will create the below code:
CreateTable(
"dbo.MyFlashCardPic",
c => new
{
Id = c.Int(nullable: false, identity: true),
MyFlashCardId = c.Int(nullable: false),
MyFlashCard_Id = c.Int(),
MyFlashCard_Id1 = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.MyFlashCard", t => t.MyFlashCard_Id)
.ForeignKey("dbo.MyFlashCard", t => t.MyFlashCard_Id1)
.ForeignKey("dbo.MyFlashCard", t => t.MyFlashCardId, cascadeDelete: true)
.Index(t => t.MyFlashCardId)
.Index(t => t.MyFlashCard_Id)
.Index(t => t.MyFlashCard_Id1);
I only have MyFlashCardId column in MyFlashCardPic table but it want to create another column like: MyFlashCard_Id, MyFlashCard_Id1
I want to know why this happens and how prevent it?
If I delete these columns from above migration,after creating database(with update-database command) it will throws below error when I want to use MyFlashCardPic entity
Invalid column name 'MyFlashCard_Id1' , Invalid column name 'MyFlashCard_Id'
and if I don't delete these columns from migration I have problem in editing flashcard that have pics like another question I have recently
How to find out context objects that one entity is attached to?
another point is that without
[ForeignKey("MyFlashCard")]
attribute it will create 3 index column and without
modelBuilder.Entity<MyFlashCard>()
.HasMany(e => e.MyFlashCardPics)
.WithRequired(e => e.MyFlashCard)
.HasForeignKey(e => e.MyFlashCardId)
.WillCascadeOnDelete();
in OnModeling it will create 4 index column
I think your problem is that you have both:
[ForeignKey("MyFlashCard")]
public int MyFlashCardId { get; set; }
and
public virtual MyFlashCard MyFlashCard { get; set; }
When adding the public virtual property you are prompting EF to set a foreign key relationship, which by default it does so with ClassNameId syntax. Because you have already created the FK yourself with the same name, it nevertheless still thinks it has to do something, so it creates another with 1 as a suffix. To get around the problem, remove your own ForeignKey entry and let EF do its stuff!
EDIT
I created a new MVC solution. Did the initial database create, then added a new class containing:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace MVCef.Models
{
[Table("MyFlashCard")]
public class MyFlashCard
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public MyFlashCard()
{
MyFlashCardPics = new HashSet<MyFlashCardPic>();
}
public int Id { get; set; }
public int? FaslID { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MyFlashCardPic> MyFlashCardPics { get; set; }
}
[Table("MyFlashCardPic")]
public class MyFlashCardPic
{
public int Id { get; set; }
public virtual MyFlashCard MyFlashCard { get; set; }
}
}
Made appropriate changes in IdentityModel, created new migration and everything worked, as advertised - including foreign key creation. I suggest you try the same with a new solution. I think somewhere along the line, you have confused the hell out of EF!
You should either remove the [ForeignKey("MyFlashCard")] attribute or stop configuring it through model builder. Doing both together is the cause of your troubles.
How does index creation affect you in all this?
I inserted exactly your model (I only added another missing class with Id and Description properties). I inserted also the "double configuration" of the same foreign key.
These are the statements that EF 6.1.3 runs
ExecuteNonQuery==========
CREATE TABLE [FasleManJdls] (
[Id] int not null identity(1,1)
, [Description] varchar(50) null
);
ALTER TABLE [FasleManJdls] ADD CONSTRAINT [PK_FasleManJdls_873b808d] PRIMARY KEY ([Id])
ExecuteNonQuery==========
CREATE TABLE [MyFlashCardPic] (
[Id] int not null identity(1,1)
, [MyFlashCardId] int not null
);
ALTER TABLE [MyFlashCardPic] ADD CONSTRAINT [PK_MyFlashCardPic_873b808d] PRIMARY KEY ([Id])
ExecuteNonQuery==========
CREATE TABLE [MyFlashCard] (
[Id] int not null identity(1,1)
, [FaslID] int null
, [FasleManJdl_Id] int null
);
ALTER TABLE [MyFlashCard] ADD CONSTRAINT [PK_MyFlashCard_873b808d] PRIMARY KEY ([Id])
ExecuteNonQuery==========
CREATE INDEX [IX_MyFlashCardId] ON [MyFlashCardPic] ([MyFlashCardId])
ExecuteNonQuery==========
CREATE INDEX [IX_FasleManJdl_Id] ON [MyFlashCard] ([FasleManJdl_Id])
ExecuteNonQuery==========
ALTER TABLE [MyFlashCardPic] ADD CONSTRAINT [FK_MyFlashCardPic_MyFlashCard_MyFlashCardId] FOREIGN KEY ([MyFlashCardId]) REFERENCES [MyFlashCard] ([Id])
ExecuteNonQuery==========
ALTER TABLE [MyFlashCard] ADD CONSTRAINT [FK_MyFlashCard_FasleManJdls_FasleManJdl_Id] FOREIGN KEY ([FasleManJdl_Id]) REFERENCES [FasleManJdls] ([Id])
No strange columns, exactly as I'm expecting, one foreign key ([MyFlashCardPic].[MyFlashCardId] REFERENCES [MyFlashCard].[Id]).
I'm quite sure that the problem is somewhere else.
Wich EF are you using?
EDIT
Why your classes are marked as partial? Are you sure that you don't have other code around?

Unable to save new Entities with One-To-Many relation in Entity Framework

When trying to save two new entities with a One-To-Many relation, I am getting the following error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
I am using EF v6.1.3
Here is an example of the code:
var entity = _dbContext.Set<EntityA>().Create();
entity.Identifier = 1234;
entity.Title = "Test Title";
entity.Description = "Test Description";
_dbContext.Set<EntityA>().Add(entity);
var relatedEntity = _dbContext.Set<EntityB>().Create();
relatedEntity.Identifier = "123-1234";
relatedEntity.Title = "Test Title";
relatedEntity.Statement = "Sample Statement";
relatedEntity.Entity = entity;
_dbContext.Set<EntityB>().Add(relatedEntity);
_dbContext.SaveChanges();
The only way I can get this to not give the error is to call _dbContext.SaveChanges(); before creating the related entity.
Any help would be greatly appreciated.
EDIT
Here is the SQL for the tables:
CREATE TABLE [dbo].[EntityA]
(
[Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
[Identifier] NVARCHAR (100) NULL,
[Title] NVARCHAR(100) NULL,
[Description] NVARCHAR (2000) NULL
)
CREATE TABLE [dbo].[EntityB]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Identifier] NVARCHAR (100) NULL,
[Title] NVARCHAR(100) NULL,
[Statement] NVARCHAR (2000) NOT NULL,
[EntityAId] INT NOT NULL,
CONSTRAINT [FK_EntityB_EntityA] FOREIGN KEY ([EntityAId]) REFERENCES [EntityA]([Id])
)
Here is the OnModelCreating configuration:
modelBuilder.Entity<EntityA>()
.HasMany(e => e.RelatedEntities)
.WithRequired(e => e.EntityA)
.HasForeignKey(e => e.EntityAId);
Here are the Entity Classes:
[Table("EntityA")]
public class EntityA
{
public int Id { get; set; }
public string Identifier { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public virtual IList<EntityB> RelatedEntities { get; set; }
}
[Table("EntityB")]
public class EntityB
{
public int Id { get; set; }
public int EntityAId { get; set; }
public string Identifier { get; set; }
public string Title { get; set; }
public string Statement { get; set; }
}
The moment when you create the Entity2, Entity(1) is not yet in the database and Entity2 needs to have 1 available to create itself. You do right thing to call SaveChanges() to create Entity(1) first.

EF6: "entity types cannot share table" error without any table splitting

I have created an EntityFramework 6 data context (code first) to represent some tables in a database: as for the repro code quoted below, one (Items) contains the items of some kind of dictionary, another (Notes) contains some notes which can be attached to items. An item can contain 0 or more notes. Nothing difficult here: I'll just have 1 POCO object for each table, and this is what I'm doing; yet, when initializing the database from my DbContext I get the error:
"The entity types 'EfItem' and 'Item' cannot share table 'Item' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them."
Now, this error seems to be meaningful for cases where several objects are mapped to the same SQL table, but this is not the case here. I have 1 and only 1 concrete object for each of the tables. So, what could be the reason of this error?
I have created a repro solution you can download from https://1drv.ms/u/s!AMHCfliT740PkK9H, which needs further explanation.
The main point in this solution is that I need to keep the client code totally agnostic of the storage technology of choice, which might be a RDBMS, a NoSql DB, etc. To this end, in my solution I have a "core" project, a PCL, which contains the interface for a high-level repository dealing only with interfaces. For instance, I have methods like AddItem(IItem item), rather than directly using a concrete type.
This allows me to customize the concrete objects as required by the technology of choice: for instance, EF objects require "parent" navigation properties, while these would not make sense in MongoDB. Of course, the downside of this approach is having to redefine most of the concrete types, and converting them into their interface-implementing counterparts: for instance, in EF I have an EfItem object, which can be converted into IItem. Note that this object does not implement this interface, but is simply convertible to it: EF forces me to use concrete types in navigation properties (see e.g. How to use interface properties with CodeFirst), so that e.g. the item's Notes collection is of type INote in the core project, but of type EfNote in the EF project.
As for the EF6 project, I have added a couple of POCO objects to represent items and notes, and a DbContext derived class with a DbSet for each type (EfItem, EfNote). Note that these objects are plain, concrete types with no direct relationship to either any other concrete type, and often they are not even implementing the corresponding core interface. For instance, in the core project I have an Item concrete type implementing IItem, but it's completely independent from EfItem (EfItem has List<EfNote> Notes while IItem has List<INote> Notes). There is no inheritance, and no table splitting; EfItem is mapped to SQL table Item, and EfNote to Note.
Here is the essential code for my EF objects:
public class EfItem
{
public string Id { get; set; }
public string SortId { get; set; }
public string ParentId { get; set; }
public string Head { get; set; }
public short ElementCode { get; set; }
public int LegacyNumber { get; set; }
public int Flags { get; set; }
public string Lemma { get; set; }
public short Homograph { get; set; }
public string Content { get; set; }
public DateTime TimeCreated { get; set; }
public string CreatorId { get; set; }
public IList<EfNote> Notes { get; set; }
}
public class EfNote
{
public string Id { get; set; }
public string ItemId { get; set; }
public Item Item { get; set; }
public string BookmarkId { get; set; }
public string UserId { get; set; }
public string Text { get; set; }
public byte Level { get; set; }
public DateTime TimeCreated { get; set; }
}
Here is my EF context:
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Conventions.Remove<PluralizingTableNameConvention>();
// item
mb.Entity<EfItem>().ToTable("Item");
mb.Entity<EfItem>().Property(i => i.Id).IsFixedLength().HasMaxLength(32)
.IsRequired().IsUnicode(false);
mb.Entity<EfItem>().Property(i => i.SortId).HasMaxLength(500)
.IsRequired().IsUnicode(false);
mb.Entity<EfItem>().Property(i => i.ParentId).IsFixedLength().HasMaxLength(32)
.IsUnicode(false);
mb.Entity<EfItem>().Property(i => i.Head).HasMaxLength(50).IsUnicode(true);
mb.Entity<EfItem>().Property(i => i.ElementCode).IsRequired();
mb.Entity<EfItem>().Property(i => i.LegacyNumber).IsRequired();
mb.Entity<EfItem>().Property(i => i.Flags).IsRequired();
mb.Entity<EfItem>().Property(i => i.Lemma).HasMaxLength(200).IsRequired();
mb.Entity<EfItem>().Property(i => i.Homograph).IsRequired();
mb.Entity<EfItem>().Property(i => i.Content).IsRequired().IsUnicode(true)
.HasColumnType("xml");
mb.Entity<EfItem>().Property(i => i.TimeCreated)
.IsRequired()
.HasColumnType("datetime2");
mb.Entity<EfItem>().Property(i => i.CreatorId).HasMaxLength(100).IsRequired();
// notes
mb.Entity<EfNote>().ToTable("Note");
mb.Entity<EfNote>().Property(n => n.Id).IsFixedLength().HasMaxLength(32)
.IsRequired().IsUnicode(false);
mb.Entity<EfNote>().Property(n => n.ItemId).IsFixedLength().HasMaxLength(32)
.IsRequired().IsUnicode(false);
mb.Entity<EfNote>().Property(n => n.BookmarkId)
.HasMaxLength(50).IsUnicode(false).IsRequired();
mb.Entity<EfNote>().Property(n => n.UserId).HasMaxLength(100).IsRequired();
mb.Entity<EfNote>().Property(n => n.Text).HasMaxLength(2000).IsRequired();
mb.Entity<EfNote>().Property(n => n.Level).IsRequired();
mb.Entity<EfNote>().Property(n => n.TimeCreated).IsRequired()
.HasColumnType("datetime2");
base.OnModelCreating(mb);
}
The SQL tables definitions are:
CREATE TABLE [dbo].[Item](
[Id] [char](32) NOT NULL,
[SortId] [varchar](500) NOT NULL,
[ParentId] [char](32) NULL,
[Head] [nvarchar](50) NULL,
[ElementCode] [smallint] NOT NULL,
[LegacyNumber] [int] NOT NULL,
[Flags] [int] NOT NULL,
[Lemma] [nvarchar](200) NOT NULL,
[Homograph] [smallint] NOT NULL,
[Content] [xml] NOT NULL,
[CreatorId] [nvarchar](100) NOT NULL,
[TimeCreated] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Item] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
CREATE TABLE [dbo].[Note](
[Id] [char](32) NOT NULL,
[ItemId] [char](32) NOT NULL,
[BookmarkId] [varchar](50) NOT NULL,
[UserId] [nvarchar](100) NOT NULL,
[Text] [nvarchar](2000) NOT NULL,
[Level] [tinyint] NOT NULL,
[TimeCreated] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Note] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
In your WorkContext.cs
Change
public DbSet<EfItem> Items { get; set; }
to
public DbSet<EfItem> EFItems { get; set; }
Also in method
protected override void OnModelCreating(DbModelBuilder mb)
b.Entity<EfItem>().ToTable("EFItem");
EF follows some naming convention between C# class and DB table name, since you have created a class named 'Item', i am afraid EF mixed class Item and EFItem together.

EF Code First Many-to-Many not working

Using the Entity Framework Code First paradigm I have defined the following objects and relationships as part of an ASP.Net MVC3 application. The problem is the many-to-many relationship between the Photo and Gallery objects is not working properly.
Currently a gallery can contain many photos - this is correct. But a photo can only be associated with one Gallery - this is incorrect. I want a photo to be able to be in many galleries. If I add a photo to a gallery when it is already associated with another gallery, it is removed from the other gallery.
I would very much appreciate your solutions on how to make this many-to-many relationship work.
Here's my code:
public class Photo
{
public int ID { get; set; }
public string Title { get; set; }
public virtual ICollection<Gallery> Galleries { get; set; }
}
public class Gallery
{
public int ID { get; set; }
public string Title { get; set; }
public virtual ICollection<Photo> Photos { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
/* I would expect the following to cause a lookup table to
* be created but it isnt */
modelBuilder.Entity<Photo>().HasMany<Gallery>(p => p.Galleries);
modelBuilder.Entity<Gallery>().HasMany<Photo>(g => g.Photos);
}
I am using the following code within my own custom database initializer.
public void InitializeDatabase(PhotoDBContext context)
{
var dbCreationScript = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
Log(dbCreationScript);
context.Database.ExecuteSqlCommand(dbCreationScript);
}
By logging the dbCreationScript variable I can see that the relevant sql that is generated for these tables is as follows.
create table [dbo].[Galleries] (
[ID] [int] not null identity,
[Title] [nvarchar](max) null,
[Photo_ID] [int] null,
primary key ([ID])
);
create table [dbo].[Photos] (
[ID] [int] not null identity,
[Title] [nvarchar](max) null,
[Gallery_ID] [int] null,
primary key ([ID])
);
alter table [dbo].[Photos] add constraint [Gallery_Photos] foreign key ([Gallery_ID]) references [dbo].[Galleries]([ID]);
alter table [dbo].[Galleries] add constraint [Photo_Galleries] foreign key ([Photo_ID]) references [dbo].[Photos]([ID]);
As you can see there is no SQL generated to create a lookup table which is probably why the relationship is not working - why isnt a lookup table being created?
I believe you need to use something like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Photo>()
.HasMany<Gallery>(x => x.Galleries)
.WithMany(x => x.Photos)
.Map(x =>
{
x.MapLeftKey("ID");
x.MapRightKey("ID");
x.ToTable("GalleryPhotos");
});
}

Categories