I have an interesting problem to solve, but although it is common, it seems like it is not easily reachable with the Entity Framework. There are two tables:
Player(Id,TeamId,FirstName,LastName) Team(Id, Name, IsProfessional)
A player may belong to only one team. Using TPT (first DB), we have two classes mapped to these tables:
public class Player { public int Id{get;set;} public int TeamId{get;set;} public string FirstName{get; set;} public string LastName{get; set;} public Team Team{get;set;} } public class Team { public int Id{get; set;} public string Name{get;set;} public bool IsProfessional{get;set;} public IEnumerable<Player> Players{get;} }
What I would like to get is the IsProfessional property on the Player object:
public class Player { public int Id{get;set;} public int TeamId{get;set;} public string FirstName{get; set;} public string LastName{get; set;} public Team Team{get;set;} **public bool IsProfessional{get;}** should be read-only }
Can the mapping be configured this way IsProfessional property can be used in linq queries?
var result= db.Players.Where(p=>p.IsProfessional==true);
and so that this field is populated every time the Person object materializes?
Player pl = db.Players.Where(p=>p.FirstName="Lionel").FirstOrDefault(); if(pl.IsProfessional) {
Already tried with:
- Separation of objects . Impossible, because I want to save the mapping of the Team and because the ratio is not 1: 1)
- Matching a Player object with a db view . I did not like it, because there are other relationships that I have with the subject. I know that they can be created manually, but updating edmx from the database will reset ssdl.
thanks
Decision
Based on the second Gert Arnold answer, the solution that fits my needs is as follows:
I create the GetIsProfessional function (she had to do this because the calculated fields can usually be done only from the tableβs own fields)
CREATE FUNCTION [dbo].[GetIsProfessional](@teamId as INT) RETURNS bit BEGIN DECLARE @isProfi AS bit SELECT @isProfi = IsProfessional FROM Teams WHERE Id = @teamId RETURN @isProfi END
I created a computed field in the Player table
ALTER TABLE Players ADD [IsProfessional] AS dbo.GetIsProfessional(TeamId)
Since I use the first db approach, I just update the model from the database and what I can request in this field and pre-populate it when the Player object materializes.
source share