How to make RIA ignore a property when generating entities? - c#

Background: Silverlight 4 ("Library" silverlight project) RIA connected to Entity Framework Model (Library.Web web application project).
Description:
I have a class:
public class Book
{
[Key]
public int ID { get; set; }
public String Name { get; set; }
public DateTime DatePublished { get; set; }
// I don't need this one in SL4
public BookInfo Info { get; set; }
}
When I try to compile, RIA generates the following error:
Entity 'MyCompany.Library.Book' has a property 'Info' with an
unsupported type.
Question:
I don't need that property in SL4 application so what I want to know is how to prevent the RIA from trying to generate that property when generating the proxy object?

public class Book
{
[Key]
public int ID { get; set; }
public String Name { get; set; }
public DateTime DatePublished { get; set; }
// I don't need this one in SL4
[Exclude]
public BookInfo Info { get; set; }
}
Using the Attribute [Exclude] ria wont pass that property to the client side and it will only be visible on the server side i think this is what you are looking for :D

My error occurred because I needed a default constructor for my property. In your case it maybe that you need a default constructor for BookInfo

Related

EF Navigational Property through Different Properties Other Than Key

I'm working on mapping a legacy application with classes and use EntityFramework against it.
One flaw I have found in this legacy database is that multiple tables refer to a specific table through 2 different fields.
I'm not sure if this is possible and why I can't seem to find anything about it so I am here.
Here is a visual sample:
public class Term {
[Key]
public string Id { get; set; } // sample value: "12-34-56/78"
public string CleanId { get; set; } // sample value: "12345678" (basically the Id without special characters)
public DateTime Date { get; set; }
}
public class App {
public int Id { get; set; }
public string CleanTermId { get; set; } // foreign key is in Term class using the `CleanId` field
}
public class Question {
public int Id { get; set; }
public string TermId { get; set; } // foreign key is in Term class using the `Id` field
}
How can I properly add a navigational property from App and Question to the Term class using either DataAnnotations (preferred) to Fluent API? I do not require a navigational property from Term to App or Question but it's ok if your answer includes it.
Let me know if this is not clear.
Joining on fields other than Primary Key was something that isnt supported in EF versions prior to EF Core, however with your mention of it being a legacy app I doubt you would want to overhaul it to be able to use EF Core.
There was a User Voice request for the feature to be added Here which the response is that they had no plans to add this functionality into EF6 - so Core would be the only way to really do this.
In terms of your classes you would be able to link Question and Term as its based PK - FK, but the App to Term is basing both on non-PK fields, even with a Unique constraint on the DB, this is something not supported in EF prior to Core
Hi this is the correct Code:
public class Term
{
[Key]
public string Id { get; set; }
public string CleanId { get; set; }
public DateTime Date { get; set; }
}
public class App
{
public int Id { get; set; }
[ForeignKey("CleanTermId")]
public Term MyTerm { get; set; }
public string CleanTermId { get; set; }
}
public class Question
{
public int Id { get; set; }
[ForeignKey("TermId")]
public Term MyTerm { get; set; }
public string TermId { get; set; }
}

Entity Model + Scalar Property + Custom Type

When using entity framework's model designer is it possible to add a property to an entity that is not one of the standard types?
I have these two entities. The one, VirusDescription I would like to add another property which is a class I wrote however when you go to change the type of the property it only gives you basics... i.e. strings, int16...etc. Is there a way to include custom types in the designer?
I can go into the code that the designer generates and just add it myself and everything works fine but I would like the code and the designer to be consistent.
Here is the class definition for the VirusDescription entity which I updated by hand. If there is a way to update the designer from the corresponding code that would work too.
namespace Trojan.Database
{
using System;
using System.Collections.Generic;
public partial class VirusDescriptionItems
{
public string ItemId { get; set; }
public string VirusId { get; set; }
public bool On_Off { get; set; }
public System.DateTime DateCreated { get; set; }
public short AttributeId { get; set; }
public short CategoryId { get; set; }
public virtual Attribute Attribute { get; set; } //Added
public virtual Category Category { get; set; } //Added
}
}
You can create a complex type within the entity model browser and extend the generated class using partial implementations.

Entity framework: automatic deletion of parent entity

Consider the following model classes:
public class Thing
{
public int Id { get; set; }
[Required]
public Text Subject { get; set; }
[Required]
public Text Body { get; set; }
}
public class Text
{
public int Id { get; set; }
public string Value { get; set; }
}
The model is simple - each Thing must reference to two Text entities. Each Text entry at any point in time should be referenced only by a single entity of any other type (Thing is not the only one).
Is it possible to configure EF5 to automatically delete all referenced Texts when Thing gets deleted (via context.Set<Thing>().Remove), or should it be done with a database trigger?
You just need to configure CASCADE DELETE at database level and don't have to do anything special at Entity framework level.

WCF - Entity framework, generated entities serialization error

I'm new to WCF and today I have encountered a problem with DataContracts. I'm getting exception when objects are returned to client from WCF Service.
SvcTraceViewer shows the next exception:
Type
'System.Data.Entity.DynamicProxies.Person_7C797A477DD73534D4E8E743E1FCC1C75DAB75933D03B845A097A8B83F2DD748'
with data contract name
'Person_7C797A477DD73534D4E8E743E1FCC1C75DAB75933D03B845A097A8B83F2DD748:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies'
is not expected. Consider using a DataContractResolver or add any
types not known statically to the list of known types - for example,
by using the KnownTypeAttribute attribute or by adding them to the
list of known types passed to DataContractSerializer.
I have several projects in solution.
DataAccess (EntityFramework EDMX file is placed here)
Model (Entities generated by DbContextGenerator are here)
WCF Service (DataContracts are implemented in Model project)
WPF Application (runs ServiceHost)
Console Application (calls WCF project or service hosted in WPF Application)
Here is operation contract in WCF:
[OperationContract]
Person[] GetAllPersons(int version);
interface implementation:
public Person[] GetAllPersons(int version)
{
return StorageService.GetAllPersons(version);
}
The excepion is thrown when the data is recieved on the client side (ConsoleApp).
I guess the problem is related to generated entities, because they are partial classes
Here is Person class:
public partial class Person
{
public Person()
{
this.Project = new HashSet<Project>();
}
public int Id { get; set; }
public Nullable<long> AddressId { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string FirstName { get; set; }
public Nullable<long> GeoLocationId { get; set; }
public string FullGeoLocationName { get; set; }
public Nullable<long> SupervisorId { get; set; }
public Nullable<long> PositionId { get; set; }
public string Position { get; set; }
public string Office { get; set; }
public string NativeName { get; set; }
public string Location { get; set; }
public string FullName { get; set; }
public Nullable<long> PmcPersonId { get; set; }
public virtual ICollection<Project> Project { get; set; }
public virtual PersonDataVersion DataVersion { get; set; }
public virtual Workspace Workspace { get; set; }
}
I tried to mark class and it's members with [DataContract] and [DataMember] attributes, but error still happens. [KnownType(typeof(Person)] attribute also didn't help.
Is it possible to use generated entities as data contracts?
DynamicProxies indicate that you are using lazy loading and the error could be caused by the context being closed when WCF tries to serialize the object.
Try disabling lazy loading and use eager loading instead.
You should really be mapping the Person objects to data transfer objects or Poco objects. You can decorate these properties with DataMember attributes accordingly. If you must disable lazy loading you lose the benefits of the ORM and queries will be run for data you may not even use - may not be a big issue in a small system, but as systems grow it can bring them down to their knees.

Entity Framework Code First, DTO and n-tier scenario (WCF)

I've read several articles about bunch of EF and DTO, and I need some clarification about using EF Code First and DTO in n-tier scenario with WCF.
Let's look a these classes:
public class Order
{
public int Id { get; set; }
public DateTime ShipDate { get; set; }
public ObservableCollection<OrderDetail> Details { get; private set; }
}
public class OrderDetail
{
public int Id { get; set; }
public int OrderId { get; set; }
public int ProductId { get; set; }
public decimal Quantity { get; set; }
}
When user want to edit existing order, my client application (WPF MVVM app) requests some DTO, which then being converted to Order instance. Then, user makes some changes in order through UI - e.g., changes ShipDate, removes two positions, modifies one, and adds one.
Now I want to deliver changes to the server. As far as I understand DTO concept, I need to construct some DTO type, containing info about changes has been made:
[DataContract]
public class UpdateOrderDTO
{
[DataMember]
public DateTime ShipDate { get; set; }
[DataMember]
public Collection<OrderDetail> NewDetails { get; private set; }
[DataMember]
public Collection<OrderDetail> ModifiedDetails { get; private set; }
[DataMember]
public Collection<OrderDetail> DeletedDetails { get; private set; }
}
But when, and where should I to create this DTO? I mean, I can't create it on submitting changes - there's no change tracking information in Order class.
Looks like, this object have to be created together with Order after it was requested for edition by user. This allows to track changes... Am I wrong?
Please note, that the question isn't about STEs. For some reasons, I don't want/can't use them in current project.
Thanks a lot for sharing your experience.

Categories

Resources