How to map two One or Zero to One relationships - entity-framework

I have three entities with the name of SellinRequest, MortgageAndRent and Album. Each of SellinRequest and MortgageAndRent may have an Album.
public class SellingRequest
{
public int Id { get; set; }
public Album Album { get; set; }
}
public class MortgageAndRent
{
public int Id { get; set; }
public Album Album { get; set; }
}
public class Album
{
public int Id { get; set; }
public SellingRequest SellingRequest { get; set; }
public int SellingRequestId { get; set; }
public MortgageAndRent MortgageAndRent { get; set; }
public int MortgageAndRentId { get; set; }
public List<Photo> Photos { get; set; }
}
This is the logic I want to be implemented:
(SellingRequest 1 .... 0-1 Album)
(MortgageAndRent 1 .... 0-1 Album)
With these mappings:
public class SellingRequestMap : EntityTypeConfiguration<SellingRequest>
{
public SellingRequestMap()
{
this.ToTable("SellingRequest");
this.HasKey(sR => sR.Id);
// Each SellingRequest may have one Album. (SellingRequest 1 .... 0-1 Album)
this.HasOptional(sR => sR.Album).WithOptionalPrincipal(a => a.SellingRequest).WillCascadeOnDelete(false);
}
}
public RentAndMortgageMap()
{
this.ToTable("MortgageAndRent");
this.HasKey(mR=>mR.Id);
// Each MortgageAndRent may have one Album. (MortgageAndRent 1 .... 0-1 Album)
this.HasOptional(sM => sM.Album).WithOptionalPrincipal(a => a.MortgageAndRent).WillCascadeOnDelete(false);
}
But I couldn't get the result. I don't know how to relate these two tables to Album table!

Here is how I would do it w/o the fleunt syntax. Note the new id fields and that I marked the album properties as virtual
public class SellingRequest
{
public int Id { get; set; }
public int AlbumId {get;set;}
public virtual Album Album { get; set; }
}
public class MortgageAndRent
{
public int Id { get; set; }
public int AlbumId {get;set;}
public virtual Album Album { get; set; }
}
public class Album
{
public int Id { get; set; }
public int SellingRequestId { get; set; }
public int MortgageAndRentId { get; set; }
public virtual SellingRequest SellingRequest { get; set; }
public virtual MortgageAndRent MortgageAndRent { get; set; }
public virtual List<Photo> Photos { get; set; }
}

Related

Two class to the same class, one to many relationship

I would like to bring the posts belonging to profiles or pages.
How can I relate to the same class other than two classes.
What do you recommend for an arrangement like?
please help me.
class Profile
{
public int ID { get; set; }
public string Name { get; set; }
public List<Post> Posts { get; set; }
}
class Page
{
public int ID { get; set; }
public string Name { get; set; }
public List<Post> Posts { get; set; }
}
class Post
{
public int ID { get; set; }
public string Description { get; set; }
public ProfileOrPage MyProperty { get; set; } //<<< My Problem Property
}
public void GetProfilePost()
{
List<Post> postList = new List<Post>();
postList.Add(new Post
{
ID = 1,
new ProfileOrPage { // My Problem Property
ID = 3
}
});
Profile p = new Profile();
p.Posts = postList;
}
You can use inheritance.
class AbstractPage
{
public int ID { get; set; }
public string Name { get; set; }
public List<Post> Posts { get; set; }
}
class Profile : AbstractPage{}
class Page : AbstractPage{}
class Post
{
public int ID { get; set; }
public string Description { get; set; }
public AbstractPage MyProperty { get; set; }
}

ViewModel To Model Use ExpressMapper List<object> to List<Model> as Field

My Model Is :
public class Product
{
public int id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int id { get; set; }
public string Name { get; set; }
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
}
And View Model Is :
public class ProductViewModel
{
public int id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public List<string> Tags { get; set; }
}
im using ExpressMapper To Mapping.
could it be map productviewModel List Tags To public ICollection Tags?
You can register your mappings like that:
Mapper.RegisterCustom<Tag, string>((tag) => tag.Name);
Mapper.Register<Product, ProductViewModel>();
Mapper.Compile();
Here is working example: https://dotnetfiddle.net/2r7l4z

Entity framework navigation property is null

I have two models using Entity Framework.
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Plays { get; set; }
public string FavouriteSurface { get; set; }
}
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int Player1Id { get; set; }
public int Player2Id { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public List<Player> Players { get; set; }
}
I am using the below code to attempt to display the Name of the player, based on the PlayerId in the SinglesMatch model matching the PlayerID from the Player model.
#foreach (var item in #Model)
{
<ul id="Players" class="bg-success"></ul>
<br/>
<h3>Date - #Html.DisplayFor(#modelItem => item.Date)</h3>
<li>Venue - #Html.DisplayFor(#modelItem => item.Venue)</li>
<li>Player 1 - #Html.DisplayFor(#modelItem => item.Players.First(p => p.PlayerId == item.Player1Id).Name)</li>
<li>Player 2 - #Html.DisplayFor(#modelItem => item.Players.First(p => p.PlayerId == item.Player2Id).Name)</li>
<li>Score- #Html.DisplayFor(#modelItem => item.Score)</li>
}
Upon debugging, the navigation property is always showing as null when the model is retrieved from my repository.
Am I using the navigation property in the correct fashion ? is there a problem with my query ?
Edit to include DbContext:
public TennisTrackerContext() : base("name=TennisTrackerContext")
{
}
public DbSet<Player> Players { get; set; }
public DbSet<PlayerRecord> PlayerRecords { get; set; }
public DbSet<SinglesMatch> SinglesMatches { get; set; }
public DbSet<DoublesMatch> DoublesMatches { get; set; }
public DbSet<Venue> Venues { get; set; }
}
}
You need to add a bridge table. Sql will create this automatically but you won't have access to the variables unless you create it in c#.
public class Player
{
public int PlayerId { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public string Plays { get; set; }
public string FavouriteSurface { get; set; }
List<PlayerInMatch> Matches { get; set; }
public Player()
{
Matches = new List<PlayerInMatch>();
}
}
public class PlayerInMatch
{
public int Id { get; set; }
public int PlayerId { get; set; }
[ForeignKey("PlayerId")]
public Player Player { get; set; }
public int SinglesMatchId { get; set; }
[ForeignKey("SinglesMatchId")]
public SinglesMatch SinglesMatch { get; set; }
}
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public List<PlayerInMatch> Players { get; set; }
public SinglesMatch()
{
Players = new List<PlayerInMatch>();
}
}
static void Main(string[] args)
{
var match = new SinglesMatch();
match.Players.Select(c => c.Player.Name);
}
You need to make your navigation property virtual to enable lazy/eager loading:
public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int Player1Id { get; set; }
public int Player2Id { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }
public virtual List<Player> Players { get; set; }
}
Also, did you define the relationship between SinglesMatch and Singles in fluent api?
EDIT: I see you don't have any relations mapped through annotations or fluent api whatsoever, I suggest you take a look at this:
https://msdn.microsoft.com/en-us/data/jj591617.aspx

Entity Framework Code first adds unwanted foreign key column

I have a many to one relationship between Categories and News.
The problem I'm having is that EF keeps adding a foreign key colum to my table which I dont want!
News class
public class News
{
public News()
{
}
[Key]
public int NewsID { get; set; }
public int PublishedByID { get; set; }
public string PublishedByFullName { get; set; }
public string PublishedByEmail { get; set; }
public DateTime DatePublished { get; set; }
public string Title { get; set; }
public string PreviewText { get; set; }
public string BlobName { get; set; }
public virtual Category Category { get; set; }
}
Categories class
public class Category
{
public Category()
{
News = new HashSet<News>();
}
[Key]
public int CategoryID { get; set; }
public string Name { get; set; }
public string CategoryTypeName { get; set; }
public virtual ICollection<News> News { get; set; }
}
Database
My question
How do I remove Category_CategoryID in News table?
I'm guessing i'm missing some code in my OnModelCreating method.
You need to add id field in News class to reference Category.
public class News
{
[Key]
public int NewsID { get; set; }
public int CategoryID { get; set; } // added
public int PublishedByID { get; set; }
public string PublishedByFullName { get; set; }
public string PublishedByEmail { get; set; }
public DateTime DatePublished { get; set; }
public string Title { get; set; }
public string PreviewText { get; set; }
public string BlobName { get; set; }
public virtual Category Category { get; set; }
}

Code first fluent api - table relationship

I have following tables and need to set relationship between them.
Model classes for the tables are as given
public class UserAction
{
public int ActionID { get; set; }
public string ActionName { get; set; }
public virtual ICollection<RoleScreenActionPermission> RoleScreenActionPermissions { get; set; }
}
public class Screen
{
public int ScreenID { get; set; }
public string ScreenName { get; set; }
public virtual ICollection<RoleScreenActionPermission> RoleScreenActionPermissions { get; set; }
}
public class ScreenAction
{
public int ScreenActionID { get; set; }
public int ScreenID { get; set; }
public int ActionID { get; set; }
public virtual Screen Screen { get; set; }
public virtual UserAction UserAction { get; set; }
}
public class RoleScreenActionPermission
{
public int RoleScreenActionPermissionID { get; set; }
public int ScreenActionID { get; set; }
public int RoleID { get; set; }
public virtual ScreenAction ScreenAction { get; set; }
public virtual Role Role { get; set; }
}
The talbe structure created is as:
Please help with setting the relationship correctly.
Try to remove all your own foreign keys from your classes. EF must make it.
upd:
public class Screen
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Action
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ScreenAction
{
public int Id { get; set; }
public virtual Screen Screen { get; set; }
public virtual Action Action { get; set; }
}
public class RoleScreenActionPermission
{
public int Id { get; set; }
public virtual ScreenAction ScreenAction { get; set; }
public virtual Role Role { get; set; }
}