I have seen strange behavior when I marked an object as abstract="true". how the object will behave when I marked an object as abstract="true"? what is abstract="true".
when to use abstract="true"? becuase I can inherit the property by using parent="object id" without marked parent object as abstract="true".
the strange behaviour:
When I am refering the abstract marked object, spring is throwing an error (Inner Exception: Spring.Core.TypeMismatchException: Cannot convert property value of type [Spring.Objects.Factory.Support.RootObjectDefinitio n] to required type)
but when I run my unit test and injecting the dependency via autowire by type using " Spring.Testing.NUnit.AbstractDependencyInjectionSp ringContextTests " then the spring is injecting the abstract marked object properly which is strange.
I have no idea what is going on?
e.g.
public class Vehicle
{
public int NoOfTyre { get; set; }
public string Color { get; set; }
public string EngineType { get; set; }
public string GearType { get; set; }
public string DrivingStyle { get; set; }
public string Manufacture { get; set; }
}
public class Car : Vehicle
{
public string DoorType { get; set; }
}
public class Scooter : Vehicle
{
public string ScooterType { get; set; }
}
Please help!
Cheers,
Milind
when to use abstract="true"? becuase I
can inherit the property by using
parent="object id" without marked
parent object as abstract="true"
The section on object definition inheritance in the Sprint.Net documentation explains it pretty well.
You use abstract="true" when you will use the object definition only to create child definitions. This may be the case because you know the definition is incomplete, because there is no corresponding .NET class, or simply to express your intent that the definition is merely a reusable template.
Related
Consider the following EF entities:
public class Location
{
[Key]
public int LocationId { get; set; }
[StringLength(50)]
[Required]
public string LocationDescription { get; set; }
}
public class LocationHolding : Location
{
[StringLength(50)]
[Required]
public string LocationDescriptionHolding { get; set; }
}
The context is derived from DbContext and has a single DbSet:
public IDbSet<Location> Location { get; set; }
Using the EntityFramework.Extended package, upon attempting a batch update:
context.Location
.OfType<LocationHolding>()
.Where(x => true)
.Update(u => new LocationHolding
{
LocationDescription = u.LocationDescriptionHolding
});
The following exception is thrown:
There are no EntitySets defined for the specified entity type 'TestEfHierarchy.Model.LocationHolding'.
If 'TestEfHierarchy.Model.LocationHolding' is a derived type, use the base type instead.
Parameter name: TEntity
Whilst having success with the package against simple single entities, I've been unable to find an examples where this has been used in more complex situations.
For the entity hierarchy, both table-per-hierarchy and table-per-type have been tried, both with the same result. The only situation I have found to work is when updating with static values (i.e. LocationDescription = "Foo") and the hierarchy is TPH.
Has anybody had similar experiences or found alternative workarounds? Appreciate that alternative methods such as stored procedures are available, but would like to use a code based/fluent approach.
I'm using ASP.NET MVC5 together with EF6 and using the code first approach.
I have a property in a model that i need to to tell EF6 is NOT a foreign key:
public class LogEntry
{
public int ID { get; set; }
public int LogDayID { get; set; }
public int LogEntryTypeID { get; set; }
public int DepartmentID { get; set; }
public DateTime Clock { get; set; }
public string Text { get; set; }
public virtual LogDay LogDay { get; set; }
public virtual LogEntryType LogEntryType { get; set; }
public virtual Department Department { get; set; }
}
[NotMapped]
public class Department
{
public int ID { get; set; }
public string Title { get; set; }
}
The model Department has the [NotMapped] as this model should not be stored in the database.
I thought this was enough to make EF6 realise that DepartmentID in LogEntry shouldn't be a foreign key.. But instead it throws me an error that 'Department' is not mapped.
EDIT: Even if i remove the DepartmentID from LogEntry it still complains with the above error.
Here's the complete error message:
"The type 'SupervisorLogWeb.Models.Department' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject."
Apparently your ComplexType is discovered as a Entity - this happens, if you decided to refactor an former Entity to a ComplexType.
The ModelBuilder will decide if an type is an Entity or not (more or less) by it's presence or absence in the DbContext.
So check if your class is still defined as DbSet inside the Context and adjust accordingly.
Add the NotMapped attribute to the DeparmentID property as well. This attribute can also be applied on properties.
When all your mappings are based on conventions, EF (or any tool) can't really tell whether you broke the convention intentionally or you made a mistake. It can apply some heuristics but it's better to fail and ask the programmer than implement an unwanted mapping.
I have an abstract class and other classes that inherit from it.
Those classes are below:
[Table("Contents", Schema="Admon")]
public abstract class Content
{
public Content()
{
this.EntryDate = DateTime.Now;
}
[Key]
public int ID { get; set; }
public string Title { get; set; }
public int? ParentID { get; set; }
[StringLength(15)]
public string InfoType { get; set; }
public DateTime EntryDate { get; set; }
public string Preview { get; set; }
public string Description { get; set; }
public string Link { get; set; }
public string Text { get; set; }
public string CategoryID { get; set; }
public int? DocID { get; set; }
public virtual Content Parent { get; set; }
public virtual ICollection<Content> Subs { get; set; }
}
public class Photo : Content { }
public class Notice : Content { }
public class Article : Content { }
public class Calendar : Content { }
My problem is that anytime i run my app it throws an exception that reads
System.MissingMethodException: Cannot create an abstract class
What can I do to rectify this error.
Thanks in advance
To use inheritance in Entity Framework, you have to implement TPH (Table per Hierarchy) or TPT (Table per Type) database structure.
With this strategy, you will be able to implement your expected behavior.
You can follow this article to implement TPH or TPT, and learn about this technology.
Hope it helps !
You cannot create an instance of an abstract class. An abstract class contains abstract members which define what a subclass should contain.
If this class must remain abstract you need to create a second class that inherits from it and implements it's members and use that class to do your processing with.
If the class doesn't have to remain abstract (I can't see why it should be but without seeing the rest of your code I can't be 100% sure) then just remove the abstract keyword.
The class Content would not compile because the your class is abstract. The MVC engine
cannot create an instance of Content directly.Unless you give it some way to know which type of Problem to instantiate, there's nothing it can do.
It is possible to create your own ModelBinder implementation, and tell MVC to use it. Your implementation could be tied to a Dependency Injection framework, for example, so that it knows to create a Content1 whenever a Content class is requested.
Perhaps I'm missing something here, but it seems that anything in the object model tree 3 or more levels down, is ignored when using TryUpdateModel.
For example (simplified):
public virtual ActionResult SomeAction(int id, FormCollection form)
{
IValueProvider vpFrom = form.ToValueProvider();
/*
At this stage, vpForm contains:
1)PropertyA
2) PropertyB.SubPropertyA
3) PropertyB.SubPropertyB.SubSubPropertyA
*/
TryUpdateModel(someObjectModel, null, null, null, vpFrom);
//The first two properties are applied, number (3) seems to be ignored
Am I missing something here? If this is just the way it is, has anyone come up with a workaround?
A quick project created with the following model.
public class TestModel {
public TestModelA A { get; set; }
public string Name { get; set; }
}
public class TestModelA {
public TestModelB B { get; set; }
public string Name { get; set; }
}
public class TestModelB {
public TestModelC C { get; set; }
public string Name { get; set; }
}
public class TestModelC {
public TestModelD D { get; set; }
public string Name { get; set; }
}
public class TestModelD {
public TestModelE E { get; set; }
public string Name { get; set; }
}
public class TestModelE {
public string Name { get; set; }
}
Here's my edit - which is essentially the same as yours
[HttpPost]
public ActionResult Edit(FormCollection form) {
IValueProvider vpFrom = form.ToValueProvider();
Models.TestModel t = new Models.TestModel();
TryUpdateModel(t, null, null, null, vpFrom);
return View(t);
}
This all works exactly as expected with all the models created properly. The only problem that I can see happening is that you possibly aren't passing the same property names back from the form. (by not using <%: Html.TextBoxFor(model => model.A.B.C.CName)%> for example)
The models require parameterless constructors. But I'm sure you would have gotten an error about that - unless you're consuming the error.
So without more information about your project it will be hard to help as a basic setup produces expected results.
I believe the problem is in one of your model classes. Check, please, if PropertyB.SubPropertyB.SubSubPropertyA is really a property but not a field. A property should have get and set accessors.
Here's my checklist:
Make sure you're getting the value back in the form request. Request["A.B.C.Name"] and etc.
All the required fields are on the form.
I had deleteOnNull issue with Linq to SQL: How to set DeleteOnNull from designer for future ref if you're using L2SQL.
i'd like to know, I have a application in asp.net mvc and nhibernate. I've read about that in the Views on asp.net mvc, shouldn't know about the Domain, and it need use a DTO object. So, I'm trying to do this, I found the AutoMapper component and I don't know the correct way to do my DTOS, for some domain objects. I have a domain class like this:
public class Entity
{
public virtual int Id { get; set; }
public virtual bool Active { get; set; }
}
public class Category : Entity
{
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
public Category() { }
}
public class Product : Entity
{
public virtual string Name { get; set; }
public virtual string Details { get; set; }
public virtual decimal Prince { get; set; }
public virtual int Stock { get; set; }
public virtual Category Category { get; set; }
public virtual Supplier Supplier { get; set; }
public Product() { }
}
public class Supplier : Entity
{
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
public Supplier() { }
}
I'd like to get some example of how can I do my DTOs to View ? Need I use only strings in DTO ? And my controllers, it should get a domain object or a DTO and transform it on a domain to save in repository ?
Thanks a lot!
Cheers
There is no guidelines on this matter and it depends on your personal chice. I have few advices that have proven useful in practice:
1. Use flat DTOs - this means that the properties of the DTO must be as primitive as possible. This saves you the need for null reference checking.
For example if you have a domain object like this:
public class Employee
{
prop string FirstName{get; set;}
prop string LastName{get; set;}
prop Employee Boss{get; set;}
...
}
And you need to output in a grid a list of employees and display information for their 1st level boss I prefer to create a DTO
public class EmployeeDTO
{
prop string FirstName{get; set;}
prop string LastName{get; set;}
prop bool HaveABoss{get;set}
prop string BossFirstName{get; set;}
prop string BossLastName{get; set;}
...
}
or something like this (-:
2. Do not convert everything to sting - this will bind the DTO to a concrete view because you'll apply special formatting. It's not a problem to apply simple formatting directly in the view.
3. Use DTOs in your post actions and than convert them to domain objects. Usually controller's actions are the first line of deffence against incorrect data and you cannot expect to be able to allways construct a valid domain object out of the user's input. In most cases you have to do some post-processing like validation, setting default values and so on. After that you can create your DTOs.