我正在使用Entity Framework Code First.我重写SaveChanges的DbContext,让我做一个"软删除":
if (item.State == EntityState.Deleted && typeof(ISoftDelete).IsAssignableFrom(type))
{
item.State = EntityState.Modified;
item.Entity.GetType().GetMethod("Delete")
.Invoke(item.Entity, null);
continue;
}
Run Code Online (Sandbox Code Playgroud)
这很好,所以对象知道如何将自己标记为软删除(在这种情况下它只是设置IsDeleted为true).
我的问题是我怎样才能使它在检索对象时忽略任何对象IsDeleted?所以,如果我说_db.Users.FirstOrDefault(UserId == id)如果该用户拥有IsDeleted == true它将忽略它.基本上我想过滤?
注意:我不想只是把&& IsDeleted == true
那就是为什么我用界面标记类,所以删除知道如何"正常工作",我想以某种方式修改检索,知道如何"正常工作"也基于那个界面存在.
所以我有一个泛型的类,它可能需要在它的方法中自己创建一个自己的实例,使用不同类型的泛型,这种类型是通过relfection获得的.
这很重要,因为这个Repository将T映射到数据库表[这是我正在编写的ORMish],如果表示T的类有一个表示ANOTHER表的集合,我需要能够实例化并将其传递给存储库[ala Inception ].
我提供的方法是为了让它更容易看到问题.
private PropertiesAttributesAndRelatedClasses GetPropertyAndAttributesCollection()
{
// Returns a List of PropertyAndAttributes
var type = typeof(T);
//For type T return an array of PropertyInfo
PropertiesAttributesAndRelatedClasses PAA = new PropertiesAttributesAndRelatedClasses();
//Get our container ready
PropertyAndAttributes _paa;
foreach (PropertyInfo Property in type.GetProperties())
//Let's loop through all the properties.
{
_paa = new PropertyAndAttributes();
//Create a new instance each time.
_paa.AddProperty(Property);
//Adds the property and generates an internal collection of attributes for it too
bool MapPropertyAndAttribute = true;
if (Property.PropertyType.Namespace …Run Code Online (Sandbox Code Playgroud) 再次尝试这个问题,因为我的第一次尝试几乎没有连贯性:p
所以我非常困惑并使用Entity Framework Code First
我有一个Forest类.
我有一个Tree类.
每个森林都有很多树
当我试图序列化时,我得到了循环引用
public class Forest
{
public Guid ID { get; set; }
public virtual List<Tree> Trees { get; set; }
}
public class Tree
{
public Guid ID { get; set; }
public Guid? ForestId {get;set;}
[ForeignKey("ForestId")]
public virtual Forest Forest {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
每个森林都有树木,但不是每棵树都在森林里.这样做时,我会遇到Multiplicity的错误
@(Html.Raw(Json.Encode(Model)))
Run Code Online (Sandbox Code Playgroud)
模型是森林的地方
如果我做ForestId了一个Guid而不是Guid?我得到循环引用错误.
我也尝试过protected override void
OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.Entity<Forest>()
.HasMany(x => x.Tree)
.WithOptional()
.HasForeignKey(y => y.ForestId);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
我理解在SQL Server中GUID是最独特的,并且碰撞的可能性很小,但同时有人必须赢得彩票,所以我觉得为这种可能性做准备是有道理的.
这是更快/更好的做法
使用一种技术,我只需插入一行并检查错误(@@ ERROR <> 0)直接分配一个新的GUID,然后重复直到我没有得到错误[我认为理论上最坏的只是一次...]
或使用这样的方法
DECLARE @MyGUID uniqueidentifier
SELECT @MyGUID = NewID()
if exists(select * from tablename where UserID=@MyGUID)
Run Code Online (Sandbox Code Playgroud)
并循环,直到我发现一个没有使用.
我喜欢第二种方法,因为我可以在后面的存储过程中使用GUID,所以我现在倾向于那个.
所以我有一个我想要循环的类的属性集合.对于每个属性,我可能有自定义属性,所以我想循环这些属性.在这种特殊情况下,我在City Class上有一个自定义属性
public class City
{
[ColumnName("OtroID")]
public int CityID { get; set; }
[Required(ErrorMessage = "Please Specify a City Name")]
public string CityName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
该属性定义如此
[AttributeUsage(AttributeTargets.All)]
public class ColumnName : System.Attribute
{
public readonly string ColumnMapName;
public ColumnName(string _ColumnName)
{
this.ColumnMapName= _ColumnName;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试遍历属性[工作正常]然后循环遍历属性时,它只是忽略属性的for循环并且不返回任何内容.
foreach (PropertyInfo Property in PropCollection)
//Loop through the collection of properties
//This is important as this is how we match columns and Properties
{
System.Attribute[] attrs =
System.Attribute.GetCustomAttributes(typeof(T));
foreach (System.Attribute …Run Code Online (Sandbox Code Playgroud) 所以我正在从一本书中实现一个项目,我有点困惑为什么我需要这些lambdas.
public class Cart
{
private List<CartLine> lineCollection = new List<CartLine>();
public class CartLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
public void RemoveLine(Product product)
{
lineCollection
.RemoveAll(p => p.Product.ProductID == product.ProductID);
}
}
Run Code Online (Sandbox Code Playgroud)
我为什么需要.RemoveAll(p=> p.Product.ProductID == product.ProductID)?它只.RemoveAll需要处理lambda表达式吗?我不知道为什么我不能使用this.Product.ProductID,我意识到Product是一个列表,p=> P.Product正在进行某种迭代并比较值直到它找到它需要的东西?
产品定义于
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个多线程应用程序,我担心2个线程访问队列
线程1将项放入队列中进行处理线程2从队列中删除要处理的项
线程1每分钟运行一次,因为它正在拉动数据的性质.线程2始终在运行,它从队列中删除一个项目并休眠100毫秒.我必须这样做,以确保我不会淹没它在项目出列时调用的服务.
我假设在添加或删除项目时,两个线程都应该锁定队列.有进一步的考虑吗?例如,假设线程1有一个锁,而线程2试图访问它.一旦锁被删除,线程2是否只知道等待并恢复?
是否最好使用ConcurrentQueue和TryDequeue,如果它失败,那么只需要100毫秒的睡眠时间?
提前致谢
我试图<select>使用jQuery 基于元素的值做一个简单的条件语句.这很好用:
var WantCategory = document.getElementById('WantCategory').value
if ((WantCategory <= 0) || (WantCategory >= 4)) {
alert('hi')
}
Run Code Online (Sandbox Code Playgroud)
这不起作用:
var WantCategory =$('select.WantCategory').val()
if ((WantCategory <= 0) || (WantCategory >= 4)) {
alert('hi')
}
Run Code Online (Sandbox Code Playgroud)
这是html部分
<label for="WantCategory">Item Category
<select id="WantCategory">
<optgroup label="Group of Options">
<option value="0"></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</optgroup>
</select>
</label>
Run Code Online (Sandbox Code Playgroud) 所以我写的是严格用于个人学习的C#中的ORM.我循环遍历数据库,其中列名称与类的属性匹配.然后我循环遍历类的属性并分配相应的数据库列值,但我遇到了从数据库列转换返回值的问题.
var PropCollection = type.GetProperties();
foreach (PropertyInfo Property in PropCollection)
{
Property.SetValue(_t, DReader[Property.Name].ToString(),null);
}
Run Code Online (Sandbox Code Playgroud)
我得到了预期的错误:
"System.String"类型的对象无法转换为"System.Int32"类型.
其中DReader只是一个返回循环内部列值的SQLDataReader,假设这个值是一个int,我该怎么把它这样抛出?
Property.GetType();
Run Code Online (Sandbox Code Playgroud)
正确地知道我需要的类型,但我如何使用它来投射DReader[Property.Name]?
我有一些伪代码用于递归算法,该算法可以找到数组中的最小数字.
这是算法.
Min(A[0..n - 1])
If n = 1 return A[0]
else
{
temp <-- Min(A[0..n - 2])
if temp <= A[n - 1]
return temp
else return A[n - 1]
}
Run Code Online (Sandbox Code Playgroud)
我不理解这个伪代码的一部分是"temp < - Min(A [0..n - 2])"行.具体为什么在递归调用中它是"n-2"而不是"n-1"?
我的另一个问题是如何在代码中实现该行.我正在使用Java.
在此先感谢您的帮助.
c# ×7
reflection ×3
asp.net ×2
.net ×1
casting ×1
code-first ×1
concurrency ×1
html ×1
java ×1
javascript ×1
jquery ×1
lambda ×1
locking ×1
orm ×1
pseudocode ×1
recursion ×1
sql-server ×1
t-sql ×1