[英]EF Code first - custom ICollection getter
我是实体框架的新手,但我仍在尝试了解如何处理它。 我碰到一个奇怪的问题。 我正在尝试为“一对多”的关系创建一个客户获取器,但是由于某种原因,它无法正常工作。
请看下面的代码:
namespace DigitalCard.Data.Models
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Identity.EntityFramework;
using DigitalCard.Data.Commons.Interfaces;
using DigitalCard.Data.Commons;
using Microsoft.AspNet.Identity;
using System.ComponentModel.DataAnnotations.Schema;
public class User : BasicDbEntry, IUser, IBasicUser
{
public User(string userName, string firstName, string lastName) : this(userName)
{
this.UserName = userName;
this.FirstName = firstName;
this.LastName = lastName;
}
public virtual string PasswordHash { get; set; }
//---> Table Attributes
[Required]
[Index("UserName", 1, IsUnique = true)]
[MaxLength(100)]
public string UserName { get; set; }
[Required]
[MaxLength(100)]
public string FirstName { get; set; }
[Required]
[MaxLength(100)]
public string LastName { get; set; }
[Required]
public int Age { get; set; }
//---> Referances to other tables
public virtual ICollection<Phone> _phones { get; set; }
[NotMapped]
public virtual ICollection<Phone> Phones
{
get { return this._phones == null ? new List<Phone>() : this._phones.Where(a => a.IsDeleted == false).ToList<Phone>(); }
set { this._phones = value; }
}
}
}
这是Phone类:
public class Phone : BasicContact
{
public Phone() : base() { }
// Table Attributes
[Required]
[IsPhoneNumber]
public string Number { get; set; }
}
public class BasicContact : BasicDbEntry
{
public BasicContact()
{
}
// Table Attributes
[Required(ErrorMessage = "Every type of contact should have a Label")]
public string Label { get; set; }
// Reference to other table
public virtual User _user { get; private set; }
[NotMapped]
public virtual User User
{
get { return this._user; }
set { this._user = value; }
}
}
public class BasicDbEntry : IBasicDbEntry
{
public BasicDbEntry()
{
this.Id = Guid.NewGuid().ToString();
this.CreatedOn = new DateTime(1900, 1, 1);
this.DeletedOn = new DateTime(1900, 1, 1);
this.ModifiedOn = new DateTime(1900, 1, 1);
}
[Key]
[Required(ErrorMessage = "Every database item should have an Id")]
public string Id { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime ModifiedOn { get; set; }
public bool IsDeleted { get; set; }
public DateTime? DeletedOn { get; set; }
}
本练习的全部重点是,当我编写UserInstance.Phones时,以某种方式告诉EF仅拉出未删除的电话号码和“ isDelete == false
”
我究竟做错了什么?
下面的代码看起来像您要躲避EF。
public virtual ICollection<Phone> _phones { get; set; }
[NotMapped]
public virtual ICollection<Phone> Phones
{
get { return this._phones == null ? new List<Phone>() : this._phones.Where(a => a.IsDeleted == false).ToList<Phone>(); }
set { this._phones = value; }
}
我建议您只保留并修改此行(因为应该这样做,默认情况下为EF)
public virtual ICollection<Phone> Phones { get; set; }
稍后在代码中,实例化DbContext时,只需为IsDeleted
添加此检查即可。 如果您担心的话,这对性能而言并不是什么大问题。 无论如何,DbContext将在第一个缓存级别(近似地告诉)在内存中具有DbSet的完整副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.