सारांशएफई 4.1, विरासत और साझा प्राथमिक कुंजी संघ => निर्दिष्ट अभिव्यक्ति की ResultType संगत नहीं है
मैं तीन श्रेणियां होती हैं:
Account
SpecialAccount
(Account
से विरासत में मिली)Profile
(0..1SpecialAccount
से संबंध)
दूसरे शब्दों में, 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 में एक बग है लेकिन अगर किसी को इसके बारे में बेहतर या कोई रास्ता पता है तो मैं किसी भी अंतर्दृष्टि के लिए सबसे आभारी हूं।
हमारे पास एक समान समस्या थी; कम से कम हमें एक ही त्रुटि संदेश मिल रहा था। .NET Framework 4.5 (4.0 से) में अपग्रेड करने के लिए यह हमारे लिए EF 4.4 –