首先,我想为我的英语道歉,而不是我最强大的一面。
问这个问题。在我当前的项目中,我与我的实体有接口,所以我可以在我的头上使用亚音速属性,我希望将来能够无缝地切换O/R映射器。无论如何,当我尝试使用接口和SimpleRepositorys类(如Single<>、All<>等)时,会遇到一个错误。我知道为什么我会收到错误信息,但我需要帮助找到一种方法来绕过它。错误消息:
System.InvalidCastException:无法将'SubSonic.DomainObjects.User‘类型的对象转换为'Core.DomainObjects.IUser’。
代码:
public IUser FindById(int id) {
`var user = _repository.Single<User>(x => x.Id == id);` `return (IUser)user;`}
如您所见,当我想要添加数据时,我已经尝试让用户命令IUser工作,但没有成功。我能做些什么才能让这件事成功?
谢谢,
蒂米
发布于 2010-03-10 12:11:36
我不认为亚音速是在这种情况下的问题。此代码将起作用:
namespace Core.Objects
{
public interface ICustomer
{
int CustomerID { get; set; }
string Name { get; set; }
}
}实际类的代码:
namespace Business.Entities
{
public class Customer: Core.Objects.ICustomer
{
public int CustomerID { get; set; }
[SubSonicStringLength(50)]
public string Name { get; set; }
}
}最后,获取客户的功能:
private static ICustomer CustomerByID(int id)
{
var repos = new SimpleRepository("Test", SimpleRepositoryOptions.None);
var customer = repos.Single<Customer>(c => c.CustomerID == id);
return (ICustomer) customer;
}https://stackoverflow.com/questions/2253194
复制相似问题