Domanda

I have a WCF data service that exposes my EF5 entity model. This particular model has two self-referencing columns. Below is the model.

public class Chunk
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    /// <summary>
    /// Gets/sets the left chunk, which is used for chaining
    /// </summary>
    public int? LeftChunk_Id { get; set; }

    /// <summary>
    /// Gets/sets the right chunk, which is used for chaining
    /// </summary>
    public int? RightChunk_Id { get; set; }

    /// <summary>
    /// Gets/set any chunk that should be rendered to the left of this chunk
    /// </summary>
    [ForeignKey("LeftChunk_Id")]
    public virtual Chunk Left { get; set; }

    /// <summary>
    /// Gets/sets any chunk that should be rendered to the right of this chunk
    /// </summary>
    [ForeignKey("RightChunk_Id")]
    public virtual Chunk Right { get; set; }
}

I also set up the relationships using Fluent.

modelBuilder.Entity<Chunk>()
    .HasOptional<Chunk>(o => o.Left)
    .WithMany()
    .HasForeignKey(o => o.LeftChunk_Id)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<Chunk>()
    .HasOptional<Chunk>(o => o.Right)
    .WithMany()
    .HasForeignKey(o => o.RightChunk_Id)
    .WillCascadeOnDelete(false);

Then there's this WCF data service that exposes the data model.

public class ContentStudio : DataService<ObjectContext>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Chunks", EntitySetRights.AllRead);

        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }

    protected override ObjectContext CreateDataSource()
    {
        ContentStudioDataContext ctx = new ContentStudioDataContext();

        var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;

        objectContext.ContextOptions.ProxyCreationEnabled = false;
        return objectContext;
    }
}

When I try to connect to this data service in a sample app, I get the following message:

Properties referred by the Dependent Role Chunk_Left_Source must be a subset of the key of the EntityType MyNamespace.Chunk referred to by the Dependent Role in the referential constraint for Relationship MyNamespace.Chunk_Left.

I get the same message repeated for Chunk.Right. This only happens in EF5. When I downgrade to EF 4.3.1, it works. I'm using VS 2012 and .NET 4.5. If anybody could help me with this I'd really appreciate it.

È stato utile?

Soluzione

So it looks like the answer to this one was to use the upgraded Microsoft.Data.Services library instead of the System.Data.Services library that comes with .NET 4.5. I hope this helps whoever else may stumble across this.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top