I have a project that gets data from the database. I made some modifications to the database fields in the model and now I am getting the error
The 'XXX' property on 'YYY' could not be set to a 'Guid' value. You must set this property to a non-null value of type 'String'.
XXX is the key of the class and is defined in the model as
public string XXX {get; set;}
The data type of this field in my database is uniqueidentifier.
What is the "equivalence" of this data type in mvc3?
EDIT
I tried changing the datatype of XXX to
public Guid XXX {get; set;}
but I got the error
The 'UnallocatedId' property on 'YYY' could not be set to a 'Guid' value. You must set this property to a non-null value of type 'String'.
As it says in the error, you need to use the Guid type:
public Guid XXX { get; set; }
Related
I recently moved across from SQLite.NET to SQLite-net-pcl due to the Android 7.0 SQlite issue.
In the process I have updated all my libraries and all is working well with regards to insert/drop etc.
The only issue I have is that every time I retrieve an item from the DB it always has an ID of 0. This is causing problems with updating items. Has anyone had this issue before?
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int ID { get; set; }
public string objectId { get; set; }
public DateTime createdAt { get; set; }
public DateTime updatedAt { get; set; }
public string Title { get; set; }
Thanks in advance.
Please try using this, this worked for me
using SQLite.Net.Attributes;
[PrimaryKey, AutoIncrement]
public int? Id { get; set; }
Had almost the same situation.
with all rows returning id of 0
class ToDo : Java.Lang.Object
{
[PrimaryKey, AutoIncrement]
public int ID { get; }
except I simply had forgotten to type set; in the property.
one thing that might help someone with this sorta problem is using the
GetMapping() method to get some info on the mapping used when CreateTable() is used to create the table.
as example:
Toast.MakeText(this, db.GetMapping<ToDo>().TableName.ToString(), ToastLength.Long).Show();
Toast.MakeText(this, db.GetMapping<ToDo>().HasAutoIncPK.ToString(), ToastLength.Long).Show();
using this I found out that AutoIncrement (HasAutoIncPK = false) wasn't being set on my table.
See if you created the table with the methods CreateComand(query) and ExecuteNonQuery(). If this is the case, create your table with the CreateTable<Type>() method. The primary key and autoincrement attributes are initialized at the time of creating the table through said method
I have been struggling with the same issue here.
I was manually creating the tables to guarantee a smoother update process moving forwards rather than using the CreateTable methods available.
The fix that I eventually stumbled upon was that I was using the wrong data type for my PRIMARY KEY column.
Wrong definition
[RecordIndexId] int PRIMARY KEY NOT NULL
Correct definition
[RecordIndexId] integer PRIMARY KEY NOT NULL
To add a little context there is a big different between the int and integer data types. Explained in this SO answer
My problem was that I had an internal set for my ID property setter. This had to be public to work correctly.
I'd add this as a comment but alas... I don't have enough rep.
I have a Sqlite database table named Movie whose columns basically store data simply in TEXT or INTEGER type (of course, Sqlite basically stores everything as just string the columns only have an affinity towards the type, if i am not wrong).
I am using Linq-to-Sqlite ORM to query my table data against a model named Movie. Below is the DDL for the table and code for the class.
CREATE TABLE Movie
(
Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
Title Text,
Rating TEXT,
IsSubtitle INTEGER
)
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public double Rating { get; set; }
public bool IsSubtitle { get; set; }
}
Now when I try to fetch the movie records from the database using the ORM, it throws an exception :
System.InvalidOperationException was caught
HResult=-2146233079
Message=The 'Rating' property on 'Movie' could not be set to a 'System.String' value. You must set this property to a non-null value of type 'System.Double'.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at LinqToSqliteDemoApp.Program.Main(String[] args) in e:\Projects\TestApplications\LinqToSqliteDemoApp\LinqToSqliteDemoApp\Program.cs:line 25
InnerException:
Obviously, it cannot cast the TEXT type of data from the Rating column into the double data type of Movie class.
So I would like to know, is there any workaround to tell the ORM to implicitly map or convert the Rating column data to double data type while retrieving from the database ?
You need to read the docs on sqlite data types http://www.sqlite.org/datatype3.html, so use double in your create table statement
I have 2 tables in an existing database:
Table1
[Key] public int Id {get; set;}
public int CustomerID {get; set;}
List<Table2> Table2Items {get; set;}
....
Table2
[Key] public int Id {get; set;}
public int customerid {get; set;}
Table1 Table1Item {get; set;}
...
I want to create a one-to-many relationship, such that each record in table1 can have many associated records in table2.
Its normally straight forward using the primary key field in table1 which matches the foreign key field (customerid) in table 2.
But I want to relate the 2 tables based on the CustomerID in table1 with the customerid in table2.
The following appears to relate the 2 tables by using the customerid field in table2 with the primary key in table1, which is not what I require.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Table1>()
.HasMany<Table2>(s => s.Table2Items)
.WithRequired(s => s.Table1Item)
.HasForeignKey(s => s.customerid);
}
How can I modify the code shown above to fit my requirements.
What you are trying to achieve is impossible with current version of Entity Framework. Quoting from https://stackoverflow.com/a/7022799/337294:
It is not possible. Relations in EF follows exactly same rules as in the database. It means that principal table must have unique identifier which is referenced by dependent table. In case of database the identifier can be either primary key or unique column(s) of principal table. Otherwise it is not valid relation.
And since Entity Framework does not support unique indexes yet (despite strong demand in their Feature Suggestion page), it has to be the Id property of your Table1 class.
I have this action which receives a parameter and i want to print out all the results
public ActionResult MusicaGenero(string genero) {
//should return more than 30 rows
var results = con.artista.Where(x=>x.genero==genero);
return View(results);
}
MusicaGenero have this
#model IEnumerable<MvcApplication1.Models.detallesMusica>
#{
ViewBag.Title = "MusicaGenero";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Musica del genero de: #ViewBag.genero</h2>
<ul>
#foreach(var detallesMusica in Model)
{
<li>#detallesMusica.artista</li>
<li>#detallesMusica.nombre</li>
<li>#detallesMusica.publicado</li>
<li>#detallesMusica.costo</li>
}
</ul>
How can but it throws an exception
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'album' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'genero' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'artista' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'albums' is based on type 'album' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'generos' is based on type 'genero' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'artista' is based on type 'artista' that has no keys defined.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'album' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'genero' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'artista' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'albums' is based on type 'album' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'generos' is based on type 'genero' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'artista' is based on type 'artista' that has no keys defined.
what is the problem here? I added already a key but still giving me that error.
As you can see, you are getting System.Data.Entity errors, and these, at least in this instance, have nothing to do with the code you've posted.
The Entity Framework needs some way of know what field it should define as the primary key.
You can tell it how to do that in two ways.
You can define a property with the name 'Id' or append 'Id' to a property having the same name as the entity.
For example, either of these would work
public class Album
{
public string Id {get; set;}
public string Name {get; set;}
}
public class Album
{
public string AlbumId {get; set;}
public string Name {get; set;}
}
EF would understand, based on the naming convention, to make the field Id or AlbumId the primary key.
The other thing you could do is use the System.ComponentModel.DataAnnotations [Key] attribute to define the key.
For example, this would make the field Name the primary key for the table.
using System.ComponentModel.DataAnnotations;
//...
public class Album
{
[Key]
public string Name {get; set;}
}
basically I am talking about the thing what we had in ASP.NET, called ViewState.
Here is the example, to keep it simple, I have a model Employee:
public class Employee
{
public string EmployeeId { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}
My company created records for employees with their EmployeeId, Name and Company.
The employee needs to enter (update) only his Phone number and Address.
When I present a form in Edit / or Create mode for employee to enter this information, EmployeeID, Name and Company outputted just as text, while Phone and Address are editable fields (Html.EditorFor), which allows to keep values entered in the Model object.
When this form is HTTP posted back, lets say, validation fails, and the form is presented to the user again to correct his input (View(model)).
However the values for EmployeeID, Name and Company are lost, since they were not defined as Html.EditorFor, like Phone and Address, and therefore are not preserved, when the submitted model is being passed again into the View.
How do I preserve (persist) in the Model those properties (that are not editable)?
Thank you.
Simply use Html.HiddenFor and store the values on the page. You could also use the mvc futures project and Html.Serialize it, but then you are mimicking viewstate when it's not necessary.