2011-07-11 10 views
5

सारांशएफई 4.1, विरासत और साझा प्राथमिक कुंजी संघ => निर्दिष्ट अभिव्यक्ति की ResultType संगत नहीं है

मैं तीन श्रेणियां होती हैं:

  • Account
  • SpecialAccount (Account से विरासत में मिली)
  • Profile (0..1 SpecialAccount से संबंध)

दूसरे शब्दों में, SpecialAccount में 0 या 1 Profiles हो सकता है। Profile में SpecialAccount होना चाहिए।

ईएफ में, इसे केवल साझा प्राथमिक कुंजी संबंध के रूप में स्थापित किया जा सकता है।

जब profile लगाने और उन्हें मैं इस त्रुटि मिलती है (उदाहरण के लिए, "प्रोफाइल जहां profile.SpecialAccount.Name == "blah" लगता है) SpecialAccount से सामान के बारे में पूछ:

{"The ResultType of the specified expression is not compatible with the required type.
The expression ResultType is 'Transient.reference[EFInheritanceTest.Account]' but
the required type is 'Transient.reference[EFInheritanceTest.SpecialAccount]'.
\r\nParameter name: arguments 1 "}

विवरण

इस कोड को समस्या दिखाता है:

namespace EFInheritanceTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     using (var context = new MyContext()) 
     { 
      var t = context.Profiles.Where(p => p.SpecialAccount.Name == "Fred"); 
      Console.WriteLine(t.Count()); 

      Console.ReadKey(); 
     } 
     } 
    } 

    public class MyContext : DbContext 
    { 
    public DbSet<Account> Accounts { get; set; } 
    public DbSet<SpecialAccount> SpecialAccounts { get; set; } 
    public DbSet<Profile> Profiles { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

     modelBuilder.Entity<SpecialAccount>().HasOptional(a => a.Profile); 
     modelBuilder.Entity<Profile>().HasRequired(p => p.SpecialAccount); 
    } 
    } 

public class Account 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

public class SpecialAccount : Account 
{ 
     public virtual Profile Profile { get; set; } 
} 

public class Profile 
{ 
    public int ID { get; set; } 
    public string Summary { get; set; } 
    public virtual SpecialAccount SpecialAccount { get; set; } 
} 
} 

जांच अब तक

मूल रूप से, अपराधी साझा प्राथमिक कुंजी संघ लगता है; जब Profile अपने SpecialAccount की तलाश में जाता है, तो इसके बजाय यह माता-पिता Account ऑब्जेक्ट प्राप्त करता है।

एकमात्र समाधान जिसे मैं देख सकता हूं उसे इस तरह बदलना है;

public class SpecialAccount : Account 
{ 
    public virtual ICollection<Profile> Profiles { get; set; } 
} 

और नहीं बल्कि डेटाबेस का उपयोग करने से कोड में नियम बनाए रखें। लेकिन यह सिर्फ बदसूरत है।

मुझे कनेक्ट पर यह संबंधित question और this bug मिला - लेकिन इसे हल किया गया है!

मुझे संदेह है कि यह ईएफ 4.1 में एक बग है लेकिन अगर किसी को इसके बारे में बेहतर या कोई रास्ता पता है तो मैं किसी भी अंतर्दृष्टि के लिए सबसे आभारी हूं।

+0

हमारे पास एक समान समस्या थी; कम से कम हमें एक ही त्रुटि संदेश मिल रहा था। .NET Framework 4.5 (4.0 से) में अपग्रेड करने के लिए यह हमारे लिए EF 4.4 –

उत्तर

4

के रूप में एक समाधान है जो अपने मॉडल परिभाषा बदले बिना काम करने के लिए लगता है कि आप एक में शामिल होने का उपयोग कर सकते हैं:

var t = from p in context.Profiles 
     join s in context.SpecialAccounts 
      on p.ID equals s.ID 
     where s.Name == "Fred" 
     select p; 
var count = t.Count(); 

या साथ विस्तार तरीके:

var t = context.Profiles 
       .Join(context.SpecialAccounts, 
        p => p.ID, 
        s => s.ID, 
        (p, s) => new { s, p }) 
       .Where(r => r.s.Name == "Fred"); 
var count = t.Count(); 

बहुत अच्छा नहीं है यही कारण है कि लेकिन तथ्य यह है कि अपने मूल क्वेरी काम नहीं करती है वास्तव में मुझे एक बग की तरह दिखता है। (मैंने ईएफ 4.1 के साथ परीक्षण किया है)

+0

के लिए तय किया गया है, अच्छा कामकाज! – Frans

संबंधित मुद्दे