Cześć. Mam do was takie pytanie o to mój model domenowy:
public class Tenure
{
public long Id { get; protected set; }
public string Name { get; protected set; }
public string Description { get; protected set; }
public decimal Rate { get; protected set; }
public string City { get; protected set; }
public string Address { get; protected set; }
public bool IsActive { get; protected set; }
public AgreementTypes AgreementTypes { get; protected set; }
public DateTime CreatedAt { get; protected set; }
public DateTime ExpirationDate { get; protected set; }
public long CategoryId { get; protected set; }
public long CompanyId { get; protected set; }
public virtual Company Company { get; protected set; }
public virtual Category Category { get; protected set; }
protected Tenure()
{
}
public Tenure(string name, string description, decimal rate, string city, int companyId, string address)
{
SetName(name);
SetDescription(description);
SetRate(rate);
SetCity(city);
SetCompany(companyId);
CreatedAt = DateTime.Now;
ExpirationDate = CreatedAt.AddDays(31);
SetActive();
SetAddress(address);
}
public void SetName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new Exception(ResponseMessage.IncorrectNameTenure);
}
Name = name;
}
public void SetActive()
{
if (ExpirationDate >= DateTime.Now) IsActive = false;
else IsActive = true;
}
public void SetAddress(string address)
{
Address = address;
}
public void SetDescription(string description)
{
if (string.IsNullOrWhiteSpace(description))
{
throw new Exception(ResponseMessage.IncorrectDescription);
}
Description = description;
}
public void SetCity(string city)
{
if (string.IsNullOrWhiteSpace(city))
{
throw new Exception(ResponseMessage.IncorrectCity);
}
City = city;
}
public void SetCompany(int companyId)
{
if (companyId <= 0)
{
throw new Exception(ResponseMessage.IncorrectCompany);
}
CompanyId = companyId;
}
public void SetRate(decimal rate)
{
if (rate < 1)
{
throw new Exception(ResponseMessage.IncorrectRate);
}
Rate = rate;
}
public string GetCompanyName() => Company.Name;
public string ConvertTenureDate()
{
const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
var ts = new TimeSpan(DateTime.UtcNow.Ticks - CreatedAt.Ticks);
double delta = Math.Abs(ts.TotalSeconds);
if (delta < 48 * HOUR) return "Nowa";
if (delta < 7 * DAY)
return ts.Days + " dni temu";
else return CreatedAt.ToString();
}
}
Kurcze dlaczego gdy chcę zwrócić companyName zwraca null ? i w repozytorium muszę dodać Include aby działało
public async Task<Tenure> GetAsync(long id)
{
return await db.Tenures.Include(x => x.Company).SingleOrDefaultAsync(x => x.Id == id);
}
bez include nie działa ale myślę że to jest złe rozwiązania bo aby pobrać jedną wartość muszę pobrać także całą listę company :( Można to naprawić ?
nobody01nobody01