i have a little Problem with one of my Controllers.
Everytime i try to call some function with parameter a 500 is thrown and while debugging i can see that the function is not even called.
First my WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Entity>("Entities");
builder.EntitySet<DataTypes>("DataTypes");
builder.EntitySet<ObjectValue>("ObjectValues");
builder.EntitySet<Attributes>("Attributes");
builder.EntitySet<Objects>("Objects");
builder.Namespace = "EAVService.Controllers";
builder.Action("FullAttributes").Returns<IHttpActionResult>()
.CollectionParameter<Attributes>("Attributes");
builder.Action("FullValues").Returns<IHttpActionResult>()
.CollectionParameter<ObjectValue>("ObjectValue");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: builder.GetEdmModel());
config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
}
}
My Entity:
[Table("ObjectValue")]
public partial class ObjectValue
{
public ObjectValue()
{
}
[Key]
[Column(Order = 0)]
public int ObjectId { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
public string Attribute { get; set; }
[StringLength(256)]
public string AttributeVal { get; set; }
public virtual Attributes Attributes { get; set; }
public virtual Objects Objects { get; set; }
}
and my Controller:
public class ObjectValuesController : ODataController
{
private EAVModel db;
public ObjectValuesController(IDbConnectionProvider provider)
{
db = new EAVModel(provider.GetDbConnection());
}
// GET: odata/ObjectValues
[EnableQuery]
public IQueryable<ObjectValue> GetObjectValues()
{
IQueryable<ObjectValue> query = db.ObjectValue.AsQueryable();
return query;
}
// GET: odata/ObjectValues(5)
[EnableQuery]
public IQueryable<ObjectValue> GetObjectValues([FromODataUri] string key)
{
IQueryable<ObjectValue> result = db.ObjectValue.Where(objectValue => objectValue.ObjectId == Convert.ToInt32(key)).AsQueryable();
return result;
}
.... }
The first Get Method is working fine.
When it comes to the second Get with a Parameter i get an Internal Server Error.
http://localhost:80/EAVServiceAPI/odata/ObjectValues(1)
Someone who can give me a hint what could be wrong?
Regards
Andre
The method with the key should return ObjectValue not IQueryable<ObjectValue> and the key parameter is wrong, it doesn't match the key on the ObjectValue object. Do you mean to have Key attributes on ObjectId and Attribute on ObjectValue? If so you need to have 2 key parameters on your GetObjectValues method with names that match the keys, otherwise remove one of the Key attributes and ensure the type of your key parameter matches the type of the key on ObjectValue.
I dont know what exactly i did wrong in my configuration but this solved my issue:
http://localhost/EAVServiceAPI/odata/ObjectValues?objectId=1
Regards
Andre
Related
i have this entity Page:
public class Page : FullAuditedEntity<int, User>, IMultiLanguageEntity<PageTranslation>
{
public string Name { get; set; }
public string Content{ get; set; }
public Page()
{
Translations = new List<PageTranslation>();
}
public virtual IList<PageTranslation> Translations { get; set; }
}
And entity PageTranslation:
[Table("PageTranslations")]
public class PageTranslation : Entity<int>, IEntityTranslation<Page>
{
public Page Core { get; set; }
public int CoreId { get; set; }
public string Language { get; set; }
public string Name { get; set; }
public string Content{ get; set; }
}
I want to update page entity with updated values and tranlsations, so I call this service:
public void UpdatePage(UpdatePageInput input)
{
var item = _pageRepository.Get(input.Id);
item.Content = input.Content;
item.Description = input.Description;
item.Title = input.Title;
item.Name = input.Name;
item.Translations.Clear(); // there is a problem
item.Translations.addRange(input.Translations);
}
When I call item.Translations.Clear() method I got this exception:
The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.
How to solve this in ABP - http://www.aspnetboilerplate.com/?
Thanks for help !
While assuming u have a DBContext:
U can declare the on delete action:
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Entity<Page>()
.HasOptional(a => a.Translations)
.WithMany()
.WillCascadeOnDelete(true);
}
did you try
_pageRepository.DeleteAll();
I am getting an error in my mvc application. I am trying to display record from my sql table using entity framework. here is the code which i am trying. But I dnt know why this code giving me error
This is my model
[Table("tbInsertMobile")]
public class tbInsertMobile
{
[Key]
public int MobileID { get; set; }
public string MobileName { get; set; }
public string MobileIMEno { get; set; }
public string mobileprice { get; set; }
public string mobileManufacured { get; set; }
}
my another class in model
public class EmployeeContext:DbContext
{
public DbSet<tbInsertMobile> usermobiles { get; set; }
}
This the code of my contoller
public ActionResult Index(int id)
{
EmployeeContext employeeContext = new EmployeeContext();
tbInsertMobile employee = employeeContext.usermobiles.Single(x => x.MobileID == id);
return View(employee);
}
and now the error which i am getting is this
The 'MobileID' property on 'tbInsertMobile' could not be set to a 'System.Int64' value. You must set this property to a non-null value of type 'System.Int32
Experts Tell me where I am getting this error
Thanks
The answer is there already in the comment.
It's trying to assign a long to an int. Change MobileID to be long. – CodeCaster
I have a similar question to the one here:
OData update controller throwing 415 Unsupported media type error.
I have an OData Web API controller that has a put request to update a Product but so that I don't expose my database entity I would like the put method to accept a ProductDTO instead. Is this possible?
At the moment, I have the following in my ProductsController:
public IHttpActionResult Put([FromODataUri] int key, ProductDTO product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != product.ID)
{
return BadRequest();
}
Product DBProduct = AsProduct(product);
db.Entry(DBProduct).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(DBProduct);
}
And this is my WebApiConfig.cs file:
builder.EntitySet<Product>("Products");
builder.EntitySet<ProductDTO>("ProductDTO");
Where Product is:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public DateTime Timestamp { get; set; }
}
And ProductDTO:
public class ProductDTO
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
But I get the following message:
The entity type 'ProductService.DTOs.ProductDTO' is not compatible with the base type 'ProductService.Models.Product' of the provided entity set 'Container.Products'. When an entity type is specified for an OData feed or entry reader, it has to be the same or a subtype of the base type of the specified entity set.
There must be a way of doing this...any ideas?
Cheers
You need to
return Updated(product);
instead of
return Updated(DBProduct);
The result needs to not only accept the ProductDTO type, but also return that same type of ProductDTO.
I do this by converting the entity back into the DTO type after it has been processed. This way you can do what Tan Junfu said in their answer:
return Updated(product);
This product could be the result of converting DBproduct back into the DTO type.
Inside of my model I have an attribute called attributes that its value is taken from the cutomized_attributes in my Database the property attributes does Not exist in the Database its just calculated value, while I'm trying to do the following I'm facing this error:
Error: System.Data.Entity.Edm.EdmEntityType: : EntityType 'JToken' has no key defined. Define the key for this EntityType
Model:
public class Restaurant
{
public int id{ get; set; }
public string name{ get; set; }
public string cutomized_attributes { get; set; }
private JObject _attributes { get; set; }
public JObject attributes
{
get
{
if (this._attributes == null)
return this._attributes = RestaurantAttributes.parseAttrString(this.cutomized_attributes);
return this._attributes;
}
set
{
this._attributes = value;
}
}
}
assuming you are using code first... within the dbcontext subclass
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Restaurant>().Ignore(x => x.attributes);
}
I got this DomainService method I'm calling from my SL ViewModel using the Invoke attribute:
[Invoke]
public ServiceModel.Recipy GetRecipyById(int recipyId)
{
return new Recipy
{
RecipyId = 1,
Name = "test",
Description = "desc",
Author = new Author
{
AuthorId = 1,
Name = "Johan"
}
};
}
The code in my ViewModel looks like this:
public RecipyViewModel()
{
context.GetRecipyById(1, RecipyLoadedCallback, null);
}
private void RecipyLoadedCallback(InvokeOperation<Recipy> obj)
{
_name = obj.Value.Name;
_description = obj.Value.Description;
_authorName = obj.Value.Author.Name;
}
The Recipy and Author POCO/ServiceModel classes:
public class Recipy
{
[Key]
public int RecipyId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[Association("Author", "RecipyId", "AuthorId")]
[Include]
public Author Author { get; set; }
}
public class Author
{
[Key]
public int AuthorId { get; set; }
public string Name { get; set; }
}
Now, the code works fine, except that the associated Author is not transfered over to the client/viewmodel, the Author property of Recipy is null. I thought that using the [Associate] and [Include] attributes would do the trick?
Thanks for any help, I'm trying hard to grok the DomainService/RIA stuff and I'm close to giving up and go "normal" WCF/REST instead :)
As I understand it, [Invoke] doesn't support complex hierarchies at the moment, so I solved it by making sure I had the correct attributes for [Include] and [Association] on the collection, and went back to using a normal RIA query method instead.