Problem ze zwracaniem wartości w modelu domenowym

0

Cześć oto mój model domenowy:

using System;
using System.Collections.Generic;
using System.Text;

namespace Work.Core.Domain
{
    public class Tenure
    {

        public Guid 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 Guid CompanyId { get; protected set; }
        public virtual Company Company { get; protected set; }


        protected Tenure()
        {

        }

        public Tenure(string name, string description, decimal rate, string city, Guid companyId)
        {
            Name = name;
            Description = description;
            Rate = rate;
            City = city;
            CompanyId = companyId;
        }
}

        public string GetCompanyName() => Company.Name; 

    }
}

Mam w niej fukcję GetCompany Name dlaczego ona zwraca null ?

0

Bo wartość Company.Name to null.

0

Tutaj zrobiłem takim samym sposobem i wszystkie funkcję zwracają odpowiednią wartość

    public class Product : Entity
    {
        protected Product() { }

        public Product(Guid id,string name, string description, decimal price, int quantity, Guid categoryId)
        {
            Id = id;
            Name = name;
            Description = description;
            Price = price;
            Quantity = quantity;
            CategoryId = categoryId;
            DateOfAddition = DateTime.Now;
            SetUserId();
            
        }


        public decimal Price { get; protected set; }
        public int Quantity { get; protected set; }
        public Guid CategoryId { get; protected set; }
        public string UserId { get; protected set; }
        public DateTime DateOfAddition { get; protected set; }
        public virtual Category Category { get; protected set; }
        public virtual User User { get; protected set; }

        public string CategoryName() => Category.Name;

        public decimal TotalValue() =>  Quantity* Price;
        public string UserName() =>  User.UserName;

        private void SetUserId()
        {
            if(HttpContext.Current.User.Identity.GetUserId() == null)
            {
                throw new Exception("Nie zalogowany użytkownik");
            }
            UserId = HttpContext.Current.User.Identity.GetUserId();
        }
    }

a tutaj mam plik konfiguracyjny

        public void Configure(EntityTypeBuilder<Tenure> builder)
        {
            builder.HasKey(x => x.Id);
            builder.Property(x => x.Name).HasMaxLength(120).IsRequired();
            builder.Property(x => x.Description).HasMaxLength(400).IsRequired();
            builder.Property(x => x.Rate).IsRequired();
            builder.Property(x => x.City).HasMaxLength(100).IsRequired();
            builder.HasOne(x => x.Company).WithMany(x => x.Tenures).HasForeignKey(x => x.CompanyId).OnDelete(DeleteBehavior.Cascade);
        }
0

Jak tworzysz te obiekty? Bo zgaduje, że robi to Entity framework. Jeżeli tak to wrzuć konfiguracje z DbContext oraz kawałek kodu, w którym pobierasz "Tenure" z DbContextu. Dodatkowo sprawdź czy w bazie danych w tabeli "Company" masz w kolumnie "Name" wartość.

0
Kokoniłaj napisał(a):

Jak tworzysz te obiekty? Bo zgaduje, że robi to Entity framework. Jeżeli tak to wrzuć konfiguracje z DbContext oraz kawałek kodu, w którym pobierasz "Tenure" z DbContextu. Dodatkowo sprawdź czy w bazie danych w tabeli "Company" masz w kolumnie "Name" wartość.

Tutaj jest plik konfiguracyjny:

        public void Configure(EntityTypeBuilder<Tenure> builder)
        {
            builder.HasKey(x => x.Id);
            builder.Property(x => x.Name).HasMaxLength(120).IsRequired();
            builder.Property(x => x.Description).HasMaxLength(400).IsRequired();
            builder.Property(x => x.Rate).IsRequired();
            builder.Property(x => x.City).HasMaxLength(100).IsRequired();
            builder.HasOne(x => x.Company).WithMany(x => x.Tenures).HasForeignKey(x => x.CompanyId).OnDelete(DeleteBehavior.Cascade);
        }

mam kurcze czyli coś źle mam w plku konfiguracyjnym ?
A tutaj pobieram z bazy:

        public async Task<Tenure> GetAsync(Guid id)
        {
            var item = await db.Tenures.FirstOrDefaultAsync(x => x.Id == id);

            return item;
        }
0

Obstawiam, że dane w bazie są tak uzupełnione. Powtarzam. Sprawdź sobie jak to wygląda u Ciebie z tabela Company. Jeżeli ef nie pobrałby Company z bazy danych to w kodzie poniżej zamiast nulla dostałbyś NullReferenceException:
public string GetCompanyName() => Company.Name;

0

kurcze tabelka wygląda tak:
https://zapodaj.net/10e348dd1f3e2.png.html

0

Ustaw sobie breakpoint w visual studio przed return item i sprawdź jak wygląda struktura obiektu. Czy Company też posiada konfiguracje?

var item = await db.Tenures.FirstOrDefaultAsync(x => x.Id == id);
return item;

1 użytkowników online, w tym zalogowanych: 0, gości: 1