Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用值注入器映射不同的属性值_C#_Sql Server 2008_Entity Framework_Inheritance_Valueinjecter - Fatal编程技术网

C# 使用值注入器映射不同的属性值

C# 使用值注入器映射不同的属性值,c#,sql-server-2008,entity-framework,inheritance,valueinjecter,C#,Sql Server 2008,Entity Framework,Inheritance,Valueinjecter,我有一个从基类派生的表。因此,派生表将具有与基表相同的id。 像 现在,虽然我可以使用ManId作为数据库中的主键,并使用fluent api让类知道ManId是基类AnimalId,但我无法在我的poco类和编程中严格使用ManId 因此,我使用了viewmodel,给出了属性名ManId,以便在类和视图中使用。我正在使用ValueInjector在模型和viewmodel之间映射 我整个上午都在寻找解决方案,我遇到的问题是: valueinjector无法将AnimalId注入ManId,因

我有一个从基类派生的表。因此,派生表将具有与基表相同的id。 像

现在,虽然我可以使用ManId作为数据库中的主键,并使用fluent api让类知道ManId是基类AnimalId,但我无法在我的poco类和编程中严格使用ManId

因此,我使用了viewmodel,给出了属性名ManId,以便在类和视图中使用。我正在使用ValueInjector在模型和viewmodel之间映射

我整个上午都在寻找解决方案,我遇到的问题是: valueinjector无法将AnimalId注入ManId,因为名称不同

我发现解决办法可能是。。使用conventioninjection覆盖默认值,但我无法正确实现它

public class PropertyMismatch:ConventionInjection
{
    protected override bool Match(ConventionInfo c)
    {
        return ((c.TargetProp.Name == "ManId" && c.SourceProp.Name == "AnimalId") ||
            (c.SourceProp.Name==c.TargetProp.Name &&                            c.SourceProp.Value==c.TargetProp.Value));

    }


}
如果有人知道解决办法,那对我应该有很大帮助。Thanx会提前向所有的观众和求解者发出通知。

试试这个:

class Program
{
    static void Main( string[] args )
    {
        Animal animal = new Animal() { AnimalId = 1, Name = "Man1" };
        Man man = new Man();
        man.InjectFrom<Animal>( animal );
    }
}

public class Animal:ConventionInjection
{
    public int AnimalId { get; set; }
    public string Name { get; set; }

    protected override bool Match( ConventionInfo c )
    {
        return ((c.SourceProp.Name == "AnimalId") && (c.TargetProp.Name == "ManId"));
    }
}

public class Man : Animal
{

    public int ManId { get; set; } 
    public string Communicate { get; set; }
}
试试这个:

class Program
{
    static void Main( string[] args )
    {
        Animal animal = new Animal() { AnimalId = 1, Name = "Man1" };
        Man man = new Man();
        man.InjectFrom<Animal>( animal );
    }
}

public class Animal:ConventionInjection
{
    public int AnimalId { get; set; }
    public string Name { get; set; }

    protected override bool Match( ConventionInfo c )
    {
        return ((c.SourceProp.Name == "AnimalId") && (c.TargetProp.Name == "ManId"));
    }
}

public class Man : Animal
{

    public int ManId { get; set; } 
    public string Communicate { get; set; }
}

是ManId还是MainId,c.TargetProp.NameThanx中有一个拼写错误用于更正我。是ManId还是MainId,c.TargetProp.NameThanx中有一个拼写错误用于更正我。非常感谢您提供的解决方案。它工作得非常好。我必须对我的解决方案做如下一个小修改:返回c.SourceProp.Name==AnimalId&&c.TargetProp.Name==ManId | | c.SourceProp.Name==c.TargetProp.Name;此外,我在Man POCO类中执行了上述覆盖,并将Animal POCO类的覆盖方法保留为未实现异常,这样,如果有人尝试其他方法,它将抛出异常。非常感谢您的解决方案。它工作得非常好。我必须对我的解决方案做如下一个小修改:返回c.SourceProp.Name==AnimalId&&c.TargetProp.Name==ManId | | c.SourceProp.Name==c.TargetProp.Name;此外,我在Man POCO类中执行了上述重写,并将Animal POCO类的重写方法与未实现的异常一起保留,以便如果有人尝试其他方法,它将抛出异常。