开发者

How to add foreign key information to code first POCO to comply with RIA Services

开发者 https://www.devze.com 2023-04-05 06:17 出处:网络
My two classes are as follows public class Client { public Guid Id { get; set; } public String Name{ get; set; }

My two classes are as follows

    public class Client
    {
        public Guid Id { get; set; }
        public String Name{ get; set; }         
        public virtual ICollection<Product> Products{ get; set; }
    }

    public class Product
    {
        public Guid Id { get; set; }
        public String Model { get; set; }
        public String Version { get; set; }
        [ForeignKey("Client_Id")]
        public virtual Client Client { get; set; }
        [ForeignKey("Client")]
        public Guid? Client_Id { get; set; }

    }

And the DbContext class is this:

    public class ClientContext : DbContext
    {
        public DbSet<Client> Clients { get; set; }
        public DbSet<Product> Products { get; set; }
    }

I have confirmed that this model works with the associated database for all the usual CRUD stuff.

I have created a RIA DoaminService class on the web site as so:

    [EnableClientAccess]
    public class ClientDomainService : DbDomainService<ClientContext>
    {
        public IQueryable<DomainModel.Product> GetProducts()
        {
            return this.DbContext.Controllers;
        }

        public IQueryable<DomainModel.Client> GetClients()
        {
            return this.DbContext.Clients;
        }
    }

When I build the silverlight application I get the error:

Unable to retrieve association information for assocation .... Only models that include foreign key information are supported.

I thought that the [ForeignKey] attributes added to the Client and Clie开发者_高级运维nt_Id properties on the Product class would satisfy the "include foreign key information" requirement.

What am I missing?


Assuming your code is complete, you are missing the other side of the Foreign key relationship... the [Key] attributes on the primary keys.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号