Skip to content

Commit

Permalink
Refactoring entity model
Browse files Browse the repository at this point in the history
  • Loading branch information
alexf2 committed Mar 9, 2016
1 parent 79cc665 commit a1b13be
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 186 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# Visual Studio 2015 cache/options directory
.vs/

# Specific
_git/
/SSL/
Expand Down Expand Up @@ -202,6 +205,7 @@ FakesAssemblies/
# Visual Studio 6 workspace options file
*.opt


### Windows ###
# Windows image file caches
Thumbs.db
Expand Down
2 changes: 1 addition & 1 deletion src/Core/DAL/Rambler.Cinema.DAL.Entities/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Rambler.Cinema.DAL.Entities
{
public class BaseEntity
{
{
[Timestamp]
public byte[] TimeStamp { get; set; }

Expand Down
12 changes: 5 additions & 7 deletions src/Core/DAL/Rambler.Cinema.DAL.Entities/ContactPerson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace Rambler.Cinema.DAL.Entities
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class ContactPerson: Person
{

{
ICollection<Phone> _phones;
public virtual ICollection<Phone> Phones
{
Expand All @@ -23,14 +22,13 @@ public virtual ICollection<Phone> Phones
public virtual int DepartmentId { get; set; }
[ForeignKey("DepartmentId")]
[Required]
public virtual Department Department { get; set; }

public virtual Department Department { get; set; }

ICollection<Cinema> _cinemas;
ICollection<Cinema> _cinema;
public virtual ICollection<Cinema> Cinemas
{
get { return _cinemas ?? (_cinemas = new List<Cinema>()); }
set { _cinemas = value; }
get { return _cinema ?? (_cinema = new List<Cinema>()); }
set { _cinema = value; }
}

string DebuggerDisplay => $"{GivenName} {MiddleName} {SurName}: {Title} in {Department?.Name}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface ICinemaDbContext
DbSet<Address> Addresses { get; set; }
DbSet<Department> Departments { get; set; }
DbSet<Person> Persons { get; set; }
DbSet<ContactPerson> ContactPersons { get; set; }
DbSet<Phone> Phones { get; set; }
DbSet<Genre> Genres { get; set; }

Expand Down
21 changes: 13 additions & 8 deletions src/Core/DAL/Rambler.Cinema.DAL.Entities/Phone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ public class Phone: BaseEntity

string DebuggerDisplay => $"{Number} is {PhoneType}";


public virtual int? PersonId { get; set; }
[ForeignKey("PersonId")]
public virtual ContactPerson Person { get; set; }

public virtual int? CinemaId { get; set; }
[ForeignKey("CinemaId")]
public virtual Cinema Cinema { get; set; }
ICollection<ContactPerson> _сontactPersons;
public virtual ICollection<ContactPerson> ContactPersons
{
get { return _сontactPersons ?? (_сontactPersons = new List<ContactPerson>()); }
set { _сontactPersons = value; }
}

ICollection<Cinema> _cinemas;
public virtual ICollection<Cinema> Cinemas
{
get { return _cinemas ?? (_cinemas = new List<Cinema>()); }
set { _cinemas = value; }
}
}
}
36 changes: 29 additions & 7 deletions src/Core/DAL/Rambler.Cinema.EntFrameworkDB/CinemaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public CinemaDbContext(DbConnection dbConnection)
public virtual DbSet<Address> Addresses { get; set; }
public virtual DbSet<Department> Departments { get; set; }
public virtual DbSet<Person> Persons { get; set; }
public virtual DbSet<ContactPerson> ContactPersons { get; set; }
public virtual DbSet<Phone> Phones { get; set; }
public virtual DbSet<Genre> Genres { get; set; }

Expand Down Expand Up @@ -91,6 +92,26 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
cs.ToTable("Cinema_ContactPerson");
});

modelBuilder.Entity<DAL.Entities.Cinema>()
.HasMany<Phone>(s => s.Phones)
.WithMany(c => c.Cinemas)
.Map(cs =>
{
cs.MapLeftKey("CinemaId");
cs.MapRightKey("PhoneId");
cs.ToTable("Cinema_Phone");
});

modelBuilder.Entity<DAL.Entities.ContactPerson>()
.HasMany<Phone>(s => s.Phones)
.WithMany(c => c.ContactPersons)
.Map(cs =>
{
cs.MapLeftKey("PersonId");
cs.MapRightKey("PhoneId");
cs.ToTable("Person_Phone");
});

//turn off cascading del. on supervisor person
modelBuilder.Entity<DAL.Entities.Cinema>()
.HasRequired(e => e.Supervisor)
Expand All @@ -99,21 +120,21 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
.WillCascadeOnDelete(false);


modelBuilder.Entity<ContactPerson>()
/*modelBuilder.Entity<ContactPerson>()
.HasMany(e => e.Phones)
.WithOptional(e=>e.Person)
.WillCascadeOnDelete(true);
.WithOptional(e=>e.ContactPerson)
.WillCascadeOnDelete(false);
modelBuilder.Entity<DAL.Entities.Cinema>()
.HasMany(e => e.Phones)
.WithOptional(e => e.Cinema)
.WillCascadeOnDelete(true);
.WillCascadeOnDelete(false);*/


/*modelBuilder.Entity<Phone>()
.HasMany<ContactPerson>(s => s.Person)
.WithOptional(c => c.Phones)
.WillCascadeOnDelete();*/
.HasRequired<ContactPerson>(s => s.ContactPerson)
.WithOptional(c => c.)
.WillCascadeOnDelete(true);*/



Expand All @@ -132,6 +153,7 @@ void RegisterRemovers()
{
f.FilmSessions.RegisterDeleteOnRemove(this);
f.Phones.RegisterDeleteOnRemove(this);
//f.Contacts.RegisterDeleteOnRemove(this);
});

this.RegisterForDelete<ContactPerson>(f =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ DbEntityValidationException ExtractErrorsUp(DbEntityValidationException ex)

void AssignDates()
{
var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));
var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified)).ToArray();

foreach (var entity in entities)
{
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a1b13be

Please sign in to comment.