0

我知道这可能是一个简单的问题,但我很新的体系结构,我想做正确的。所以,谢谢你的理解。无法插入新的记录使用实体框架6 +代码优先+温莎城堡

我也是Castle Windsor的新手,没有和Unit Of Work一起使用没有Repository模式 - 我甚至不知道我是否需要他们来解决我目前遇到的问题。

我试图做到:

我有一个名为IDomain只有一些性质和具体的POCO类实现它的接口。

IDomain接口:

public interface IDomain : IPersistentObject 
{ 
    int DomainId { get; set; } 

    string Name { get; set; } 

    [LocalizedString] 
    string ItemName { get; set; } 

    [LocalizedString] 
    string ItemDescription { get; set; } 

    int ItemValue { get; set; } 
} 

POCO类:

public class Domain : AbstractDefault, IDomain, ILocalizedEntity 
{ 
    public virtual int DomainId { get; set; } 

    public virtual string Name { get; set; } 

    [LocalizedString] 
    public virtual string ItemName { get; set; } 

    [LocalizedString] 
    public virtual string ItemDescription { get; set; } 

    public virtual int ItemValue { get; set; } 
} 

DomainService.cs类做这样的:

public void Insert(IDomain param) 
    { 
     using (var db = new DefaultContext()) 
     { 
      new DomainValidation(new DbMetaData().GetMetaData, Settings.Default.CultureName).Validate(param); 

      db.Domains.Add((Domain)param); 
     } 
    } 

另一个重要信息是我使用AOC,即拦截方法调用我的类。见我的温莎安装位:

public class Installers : IWindsorInstaller 
{ 
    public void Install(IWindsorContainer container, IConfigurationStore store) 
    { 
     container.Register(Component 
      .For<IInterceptor>() 
      .ImplementedBy<Class1>() 
      .Named("DomainInterceptor")) 

      .Register(Component 
      .For<IDomain>() 
      .ImplementedBy<Domain>() 
      .LifestyleTransient() 
      .Interceptors(InterceptorReference.ForKey("DomainInterceptor")).Anywhere); 
    } 
} 

在我的单元测试我做:

var domain = container.Resolve<IDomain>(); // Returns IDomainProxy, not IDomain 
domain.Name = "MIN_MAX"; 
domain.ItemName = new LocalizedProperty("en-US", "Mininum").Serialize(); 
domain.ItemValue = (int)MinMax.Minimum; 

new DomainService().Insert(domain); // If I try to cast by doing .Insert(domain as Domain), I get a null 

但是,当我的代码达到 “。新增(域)参数)”(DomainService.cs)我得到的错误:“无法转换类型‘Castle.Proxies.IDomainProxy’的对象键入‘Model.Domain’”。

为什么会出现这个错误,浩我是否应该修复它,考虑到我确实想要使用IoC,Windsor等?

此致敬礼。

回答

0

在那些情况下Castle Windsor使用“代理”界面创建新实例。我最终在我的Service类中使用了AutoMapper