पर संबंधित इकाइयों को जोड़ने में सक्षम नहीं है मैं टैग सहित पोस्ट की एक सूची प्राप्त करना चाहता हूं।सी # और ईएफ मॉडल
public class BlogPostGetByUrlSlugDto
{
public int Id { get; set; }
public string Title { get; set; }
public string Category { get; set; }
public string Color { get; set; }
public string UrlSlug { get; set; }
public string Description { get; set; }
public IList<BlogTagGetByPostIdDto> Tags { get; set; }
}
public class BlogTagGetByPostIdDto
{
public string Name { get; set; }
public string UrlSlug { get; set; }
}
मेरे कोड अब तक::
public BlogPostGetByUrlSlugDto GetByUrlSlug(string urlSlug)
{
var blogPostQuery = _blogPostRepository.Query;
return blogPostQuery
.Where(b => b.UrlSlug.Equals(urlSlug))
.ToList()
.Select(bp => new BlogPostGetByUrlSlugDto
{
Id = bp.Id,
Title = bp.Title,
Category = bp.BlogCategory.Name,
Color = bp.BlogCategory.Color,
UrlSlug = bp.UrlSlug,
Description = bp.Description,
Tags = bp.BlogTags.Select(t => new BlogTagGetByPostIdDto
{
Name = t.Name,
UrlSlug = t.UrlSlug
})
.ToList()
})
.Single();
}
मैं लाइन .Select(bp => new BlogPostGetByUrlSlugDto
में Object reference not set to an instance of an object
मिल
मैं मॉडल है।
कोई विचार क्यों?
भंडार_blogPostRepository.Query
के लिए है:
public interface IRepository<T> where T : class
{
T FindById(int id, bool asNoTracking = false);
T FindSingle(Expression<Func<T, bool>> predicate = null, bool asNoTracking = false, params Expression<Func<T, object>>[] includes);
IQueryable<T> Find(Expression<Func<T, bool>> predicate = null, bool asNoTracking = false, params Expression<Func<T, object>>[] includes);
/// <summary>
/// Add entity to the repository
/// </summary>
/// <param name="entity">The entity to add</param>
void Add(T entity);
/// <summary>
/// Attach entity to the repository
/// </summary>
/// <param name="entity">The entity to attach</param>
void Attach(T entity);
bool Exists(T entity);
/// <summary>
/// Updates entity within the repository
/// </summary>
/// <param name="entity">The entity to update</param>
void Update(T entity, bool partial = false);
/// <summary>
/// Mark entity by id to be deleted within the repository
/// </summary>
/// <param name="entity">The entity to delete</param>
void Delete(object id);
/// <summary>
/// Mark entity to be deleted within the repository
/// </summary>
/// <param name="entity">The entity to delete</param>
void Delete(T entity);
/// <summary>
/// Get an item matching the id
/// </summary>
T GetById(int id);
/// <summary>
/// Get an item or itens matching the Expression including opcional parameters
/// </summary>
IList<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "");
/// <summary>
/// Get an item matching to prediate
/// </summary>
//T Get(Func<T, bool> predicate);
/// <summary>
/// Get all the itens matching to prediate
/// </summary>
IList<T> GetAll(Func<T, bool> predicate);
///// <summary>
///// Get all the element of this repository
///// </summary>
///// <returns>Entities list</returns>
IList<T> GetAll();
/// <summary>
/// Allow to send Linq queries to the entity
/// </summary>
IQueryable<T> Query { get; }
/// <summary>
/// Saves the pending changes back into the DataContext.
/// </summary>
void Save();
}
क्वेरी का कार्यान्वयन:
public class Repository<T> : IRepository<T> where T : class
{
protected DbContext _dataContext;
protected DbSet<T> _dbSet;
public virtual IQueryable<T> Query
{
get
{
return _dbSet;
}
}
}
'blogPostQuery' किस प्रकार का है? या शायद मुझे पूछना चाहिए: '_blogPostRepository.Query' संपत्ति में क्या होता है? यदि आप मूल ईएफ क्वेरी वापस कर देंगे तो शायद आपके पास शून्य संदर्भ अपवाद नहीं होगा। –
हाय धन्यवाद। blogPostQuery टाइप आईआरपीओसेटरी _blogPostRepository है; जहां मेरे भंडार में मेरे पास ईएफ पर सभी ऑपरेशन हैं जैसे अटैच एक्सपैच अपडेट जोड़ें लेकिन इसमें शामिल नहीं है :( –
Patrick
आपको मूल 'IQueryable' को EF से वापस करना होगा, अन्यथा आंतरिक क्वेरी हमेशा * सभी * रिकॉर्ड वापस कर देगी। आप नहीं कर सकते अनुमानों या भविष्यवाणियों का उपयोग करें जिन्हें एसक्यूएल में वापस अनुवादित किया गया है और पेलोड को कम किया गया है और आप एकाधिक रिपॉजिटरीज़ से क्वेरी को एक तरीके से लिख नहीं सकते हैं, जिस तरह से एक SQL क्वेरी उत्पन्न होती है। और इसमें 'शामिल' नहीं है, नहीं 'AsNoTracking'। एक बड़े पैमाने पर क्वेरी परिणाम के लिए LINQ-to-ऑब्जेक्ट्स को लागू करना, इसलिए शून्य ऑब्जेक्ट संदर्भ (एक 'bp.BlogTags')। SQL में यह कोई समस्या नहीं होगी। –