我正在使用Entity Framework并触发属性更改事件,如果更改的属性已映射,我想在其中更新属性。
protected void FooPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var notMappedArray = typeof(Foo).GetProperty(e.PropertyName).GetCustomAttributes(typeof(NotMappedAttribute), false); // I thought this might return null if the property did not have the attribute. It does not.
//if (notMappedArray == null)
UnitOfWork.Value.GetRepository<Foo>().Update(MyFoo);
}
Run Code Online (Sandbox Code Playgroud)
查找发送给此事件的属性是否在实体框架中映射的最佳方法是什么?
编辑:我已经看到了这个问题。但是,答案似乎有点过头了,还不能完全满足我的需要。
我希望编译器为我推断出一种类型,但我不确定它是否可行,或者最好的选择是什么.
我想这样做:
public static TValue Get<TValue>(TKey key) where TValue : Mapped<TKey> { ... }
public class MyObject : Mapped<int> { ... }
Run Code Online (Sandbox Code Playgroud)
而C#推断TKey是一个int.有没有办法做这样的事情?如果没有,最好的选择是什么?
我想避免做类似的事情 Get<MyObject, int>(1);
编辑:
在编译我的C++程序时,我没有收到任何错误,但是在unordered_map中,哈希函数失败,尝试修改为0.(stl的hashtable_policy.h的第345行)
我找到了修复,但不知道为什么我开始时遇到问题.我的结构看起来像这样,(对不起特定的代码.)
struct Player {
private:
Entity& entity = entityManager->create();
public:
Player() {
entity.addComponent(new PositionComponent(0, 0)); // Add component uses the unordered map.
}
};
Player playerOne; // Error perpetuates through constructor.
Run Code Online (Sandbox Code Playgroud)
但是,如果我将playerOne声明为指针,就像这样:
Player* playerOne;
Run Code Online (Sandbox Code Playgroud)
然后打电话:
playerOne = new Player();
Run Code Online (Sandbox Code Playgroud)
我没有任何问题.
我一直在寻找 - 没有成功.我能做错什么?
我正在尝试找到验证MVVM中数据的最佳方法.目前,我正在尝试使用MVVM模式将IDataErrorInfo与Data Annotations一起使用.
然而,似乎没有任何工作,我不知道我可能做错了什么.我有类似的东西.
public class Person : IDataErrorInfo
{
[Required(ErrorMessage="Please enter your name")]
public string Name { get; set; }
public string Error
{
get { throw new NotImplementedException(); }
}
string IDataErrorInfo.this[string propertyName]
{
get
{
return OnValidate(propertyName);
}
}
protected virtual string OnValidate(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("Property may not be null or empty", propertyName);
string error = string.Empty;
var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
var results = new List<ValidationResult>();
var context = new ValidationContext(this, null, …Run Code Online (Sandbox Code Playgroud) 当我调用CREATE DATABASE [Hello World]SQL Server创建我的数据库时,还会自动将表添加到数据库中.这些表来自Entity Framework的迁移.
我希望在哪里阻止它在每个新数据库上创建这些表?
c# ×3
c++ ×1
generics ×1
mvvm ×1
new-operator ×1
pointers ×1
sql ×1
sql-server ×1
validation ×1
wpf ×1