i'm new to Doctrine2 and like to know how i can tell Doctrine which namespace my entities use.
My current configuration is this.
All my entities are in namespace "project\entity".
So, everytime i want to obtain the entity "Color", i have to write:
$em->getRepository("project\\entity\\Color")
How can i configure Doctrine to always use namespace "project\entity"?
You can come close to what you want by using addEntityNamespace on your config object to create a namespace alias:
$em->getConfiguration()->addEntityNamespace('NS1', 'Project\Entity');
$colorRepo = $em->getRepository('NS1:Color');
Works for queries as well.
By the way, "project\\entity\\Color" can also be written as 'project\entity\Color'. I would also suggest capitalizing Project and Entity just to conform to standards.
Related
I have a stored procedure in Mysql that returns a table.
Using of old methods like _context.database.SqlQuery doesn't work anymore.
The _context.database.Execute* only contains methods that returns number of affected rows.
In my scenario I'm using scaffolding and can't create database objects from my code, but I can create classes.
The following code (and/or similar tries with Set or Query that is obsolete)
_context.Set<MyModel>().FromSql("CALL My_USP({0});", parametervalue).ToList<MyModel>();
returns an error about that the model is not in the _context because I'm scaffolding and MyModel is a class from my Models.
I'm totally lost with this and all help I can find in S.O. or Google are about EF6, that doesn't work in my case, the libraries are different.
Any workaround will be appreciated also, if this is not possible to do.
I got a solution but I will mark as answer someone that works without the old ADO.NET or change my dbcontext like this one, because will fail the next time I will do a scaffolding.
Add to protected override void OnModelCreating(ModelBuilder modelBuilder) in the context file:
modelBuilder.Entity<MyModel>().HasNoKey();
and then call:
_context.Set<MyModel>().FromSqlRaw("CALL My_USP({0});", parametervalue).ToList<MyModel>();
I Have Create a DB in that I am Having Multiple tables having Relationship between them.
When a try to get data from my WEb app i get this error
"'Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.PrescriptionMaster_2C4C63F6E22DFF8E29DCAC8D06EBAE038831B58747056064834E80E41B5C4E4A'. Path '[0].Patient.PrescriptionMasters"
I coudn't get why i am getting this error, and when i remove the relationships between tables i get proper data From it.
I have Tried other solutions like adding
"config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore; "
in Webconfig.cs but nothing has worked for me.
Please help me, what should I do ?
The only proper way to prevent this from happening is by not sending Entity Framework objects (which may contain such loops) into the JSON Serializer (which is not too good at knowing when to stop serializing).
Instead, create ViewModels that mimic the parts of the EF Objects that your Front End actually needs, then fill those ViewModels using the EF Objects.
A quick-and-dirty way is to just use anonymous objects, for example:
return new
{
Product = new
{
Id = EF_Product.Id,
Name = EF_Product.Name
}
};
A good rule-of-thumb is to only assign simple properties (number, bool, string, datetime) from the EF Objects to the ViewModel items. As soon as you encounter an EF Object property that is yet another EF Object (or a collection of EF Objects), then you need to translate those as well to 'simple' objects that are not linked to EF.
On the other end of the spectrum there are libraries such as AutoMapper. If you decide that you need actual ViewModel classes, then AutoMapper will help mapping the EF Objects to those ViewModels in a very structured way.
Just add this to the Application_Start in Global.asax:
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter
.SerializerSettings
.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
It will ignore the reference pointing back to the object.
I am creating an ontology based on OWL/RDF/RDFS. My first ontology schema has namespaces as :
#prefix abc: https://example.com/a#
I want to change the namespace the next version of the ontology as
#prefix def: https://example-new.com/b#
But I dont want the previous users of the ontology to be affected at all. I was thinking if there is a way to define equivalent namespaces, and classify that the first name space will be deprecated. I am not sure if there is any provision in the OWL/RDF or even Dublin-core to so do.
Any help is appreciated. Thanks.
You can't do this at the namespace level but you can declare all old classes and properties as equivalent to the new ones; you'd keep the old IRIs in the equivalent axioms and declaration axioms only. Then any third part that uses a reasoner would be able to run queries just as before; parties not using a reasoner would have to rewrite their queries following equivalent axioms (something that they might already be doing for other similar use cases).
hey, there, is there any way to ass a NON DB property to a Linq-to-SQL class? (etc calculated fields), I'm getting an error when trying
Linq2Sql generates your entities as partial classes. This makes it easy to extend these classes yourself without modifying the code generated by L2S (which is a bad idea since it will be overwritten if you update your model and regenerate.)
Just find the partial class definition and create a new source file where you declare the same partial class. Inside that new class, add the property or methods that you want to use in your code but Linq2Sql will not have any knowledge of.
In LINQ to SQL, Can we inherit our entity classes from some other class or interface.
I have some common work for all my entities, which I want to code at some common place
and call it once.
Regards
Parminder
#Jason's answer is useful for manually tweaking each class. To modify all generated classes to use the same base class there is an attribute in the .dbml file that can be edited manually (no UI for it in VS2008).
Add a EntityBase xml attribute into the <Database> xml element, its value is the full name of the base class.
<Database ... EntityBase="MyNamespace.MyBaseClass" ...>
...
</Database>
Its used by the LINQ to Entity Base project.
Yes, this is possible. Use partial classes. If MyEntity is your LINQ to SQL entity class, add a partial implementation as follows:
partial class MyEntity : MyBaseClass, IMyInterface {
// do it
}
You can even make your entity class an abstract class.