Malcolm O'Hare
Malcolm O'Hare

Reputation: 4999

Is there a way to compile entityframework dataannotations in silverlight?

I am using MVC WebAPI to power a silverlight app. The POCO objects are shared between the MVC and the Silverlight. All the POCOs are in a .Net class library and I created a Silverlight class library with all the POCOs added as links so I can share the code. Unfortunately this causes issues with data annotations, as the Silverlight class library can't reference EntityFramework.dll, and therefore cannot compile attribute such as [ForeignKey].

I've ended up having wrap all my annotations like this:

#if !SILVERLIGHT
        [ForeignKey("SecurityGroupID")]
#endif
        public virtual SecurityGroup SecurityGroup { get; set; }

Has anyone figured out a way to not have the #if !SILVERLIGHT wrapping all their annotations? ([Column], [Table], [ForeignKey], etc?).

One possible solution I've considered is just creating blank attributes with matching constructors in a child namespace (MyProject.FakeAttributes) and importing that namespace in the Silverlight version. Does anyone see any problems with that solution?

Upvotes: 0

Views: 153

Answers (1)

cadrell0
cadrell0

Reputation: 17307

I have to recommend against using your Entities in Silverlight. Entities often don't play nice with WCF because of things like lazy loading and cycles in the object graph. Do yourself a favor and make dtos.

If you are not going to use dtos. I'd go with the #ifs before I'd make fake attributes.

Upvotes: 1

Related Questions