As the title says, when should we declare a property of type DbSet for an entity, General Guidelines ?
There are no general guidelines. Instance of DbSet<YourEntity> is your access point to loading and persisting entities of a given type. If you don't expose the property on your context type you can still create it on the fly by using dbContext.Set<YourEntity>().
The only difference between exposing and not exposing the property is in entity discovery during defining the model. When EF is first use it builds "the model" for defined entities. Entities are discovered through:
Mappings explicitly defined on DbModelBuilder instance
Configuration types explicitly registered in DbModelBuilder instance
DbSet<> properties defined in context type
Types referenced by already discovered entities
So if you don't use DbSet<> properties you must tell EF about your entities with other methods.
Related
I'm having difficulties understanding when to use object properties and when to use data properties. I've read the definitions, but yet I'm having issues using them in a practical setting.
It would be of much help if anyone could correct the following example.
Let's say I'm making a Wine ontology, with some subclasses redWine, whiteWin, sparklingWine and so on. How will I then manage the properties for example goesWithFoodType, fromCountry and hasGrapeType? All in which have subproperties down the hierachy. Will all of these be data properties with the domain Wine and ranges xsd:string datatype?
If the things stated above is correct, would I have any use from object properties in my ontology?
Thanks.
I have a GAE application, where I'm using the geomodel for a location based model in my database. There are two "types" of this model, however, they need to be geo-queryed together as one. The two "types" share a set of base properties, but the second type has a few more. Is there any way I can make those other properties optional rather than just setting them to bogus values?
Inside the datastore, entities are independent of each other. You can have different entities of the same Kind that have different sets of attributes. This happens very commonly if you add some new attributes on a new version of your app, and the entities that already exist in the datastore won't have those attributes.
In your code though, for any given version you end up declaring a single Model for your Kind. You can choose not to assign values to certain attributes for the different types.
Simply make sure that your code properly handles cases where attributes don't exist, or are set to None.
I am trying to clone an Entity.
Question on Entity method
Entity.setPropertiesFrom(Entity src).
The documentation says
"A convenience method that populates the properties of this Entity with the properties set on the provided Entity. This method transfers information about unindexed properties"
How about indexed properties? Do they get populated as well.
-Aswath
Yes, it transfers all properties. I suspect that the docs mean that if a property is unindexed in the source entity, it will also be marked as unindexed in the target... but I don't know for sure.
We are using Entity Framework, Code First and in our database we have several tables that have the same name but in different Schemas.
I have also put the models in two different namespaces.
How i can register these tables in my DbContext class?
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Data.Schema1.Contact>().ToTable("Contact", "schema1");
modelBuilder.Entity<Data.Schema2.Contact>().ToTable("Contact", "schema2");
}
Thanks for your help in advance!
Your classes must have different name or you must use separate context for every schema.
The reason for this is EDM model used internally. Even if you are using code-first it still creates EDM model on behind and it must follow all its restrictions and the way how POCO classes are matched to entities defined in CSDL model. Entities from EDM are and POCO classes are matched by class name (without namespaces). Because of that each class name mapped in the same context must be unique and different namespace doesn't make it different class name.
I got this problem,
"The deserializer has no knowlege of any type that maps to this contract"
After googling, I reached this post
The deserializer has no knowlege of any type that maps to this contract
where the answer says, the base class have to declare "KnownTypes" like
[DataContract, KnownType(typeof(Subclass)) ...],
If I have to declare this in my parent class, [DataContract, KnownType(typeof(Subclass))], doesn't it break the principles of OO Design that parent class doesn't have to know about subclass?
What is the right way of doing this?
The serializer is designed in a way that, if it serializes an object, it should be able to read it back. If you attempt to serialize an object with a declared type of 'Base', but an actual type of 'Derived' (see example below), if you want to be able to read back from the serialized object an instance of 'Derived', you need to somehow annotate the XML that the instance is not of the type of which it was declared.
[DataContract]
public class MyType
{
[DataMember]
public object obj = new Derived();
}
The serialized version of the type would look something like the XML below:
<MyType>
<obj actualType="Derived">
<!-- fields of the derived type -->
</obj>
</MyType>
When the type is being deserialized, the serializer will look at the "actualType" (not actual name) attribute, and it will have to find that type, initialize it, and set its properties. It's a potential security issue to let the serializer (with in Silverlight lives is a trusted assembly and has more "rights" than the normal user code) to create arbitrary type, so that's one reason for limiting the types which can be deserialized. And based on the design of the serializer (if we can serialize it, we should be able to deserialize it), the serialization fails for that reason as well.
Another problem with that is that the serialized data is often used to communicate between different services, in different computers, and possibly with different languages. It's possible (and often it is the case) that you have a class in a namespace in the client which has a similar data contract to a class in the server side, but they have different names and / or reside in different namespaces. So simply adding the CLR type name in the "actualType" attribute won't work in this scenario either (the [KnownType] attribute helps the serialzier map the data contract name / namespace to the actual CLR type). Also, if you're talking to a service in a different language / platform (i.e., Java), CLR type names don't even make sense.
Another more detailed explanation is given at the post http://www.dotnetconsult.co.uk/weblog2/PermaLink,guid,a3775eb1-b441-43ad-b9f1-e4aaba404235.aspx - it talks about [ServiceKnownType] instead of [KnownType], but the principles are the same.
Finally, about your question: does it break that OO principle? Yes, that principle is broken, that's a price to pay for being able to have lose coupling between the client and services in your distributed (service-oriented) application.
Yes it breaks the principles of OO design. This is because SOA is about sharing contracts (the C in ABC of services) and not types, whereas OO is about type hierarchies. Think like this the client for a service may not be even in an OO language but SOA principles can still be applied. How the mapping is done on server side is an implementation issue.