I am using MVC to generate my database. I have the following code structure in my UserProfile class:
[KeyAttribute()]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
This generates the database fine, but when I try to insert a record it says UserId cannot be null. I cannot figure out why it isn't auto generating an Id integer.
This was because I had some issues updating the database. Needed to manually drop this table and re-create it. Is working fine now.
Related
I am using a domain first approach. I created an asp.net core MVC project and added a Products.cs in the model folder
Products.cs
-----id
-----Name
-----Rate
Now I ran the following commands in package manager console
add-migration addedProducts
update-database
There are no errors in the console manager
A database was created in the MS-Sql server
Now I add another entity in the Models folder called Audit.cs
Audit.cs
--- public int Id { get; set; }
public string UserId { get; set; }
public string Type { get; set; }
public string TableName { get; set; }
public DateTime DateTime { get; set; }
public string OldValues { get; set; }
public string NewValues { get; set; }
public string AffectedColumns { get; set; }
public string PrimaryKey { get; set; }
Now I repeat the steps in the package manager console
add-migration audit
update-database
There are no error messages.
I dont see a table called Audit in the database.
Am I missing something?
In my ASP.Net MVC 4 project I'm trying to insert data into a table which has identity an auto-increment column (ACTIVITYID), which is the Primary Key.
Table Class
public partial class ACTIVITY
{
public decimal ACTIVITYID { get; set; }
public Nullable<decimal> CALLTICKETNUMBER { get; set; }
public string ACTION { get; set; }
public Nullable<long> STATUSIDATTHETIMEOFACTION { get; set; }
public short RECORDFORDISPLAY { get; set; }
public Nullable<System.DateTime> ACTIVITYDATE { get; set; }
public string USER_ { get; set; }
public virtual CALLTICKET CALLTICKET { get; set; }
}
Upon calling the following method generates exception
public static void AddActivity(long ticketNumber, string action, string userId, int? previousStatusId = null)
{
var newActivity = new ACTIVITY
{
ACTION = action,
ACTIVITYDATE = System.DateTime.Now,
CALLTICKETNUMBER = ticketNumber,
STATUSIDATTHETIMEOFACTION = previousStatusId,
USER_ = userId,
};
dbOraContext.ACTIVITies.Add(newActivity);
dbOraContext.SaveChanges();
}
While viewing ACTIVITies.Add(newActivity) ACTIVITYID is inserting with value 0.
On tracing the inner exception following exception is displayed:
{"ORA-00001: unique constraint (ATM_CRM.PK_ACTIVITY) violated"}
Also, an exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred.
Earlier I have worked with MS SQL Server, and never encountered such a problem. Auto-increment column should automatically get its value.
Note that I'm working on Oracle DB.
I'm trying to create a database using EF6 code-first and SQL Server Compact. After I execute update-database command I get the following result:
Screenshot SQL Server Compact
There are strange columns Operator_Id, Station_Id, Operator_Id1 and Station_Id1.
Here is my model:
public class OperatorActivity
{
public Guid Id { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public bool Synched { get; set; }
public virtual Station Station { get; set; }
public Guid StationId { get; set; }
public virtual Operator Operator { get; set; }
public Guid OperatorId { get; set; }
}
In SQL Server everything is fine
Screenshot SQL Server
Why do I get those columns in SQL Server Compact?
I think that the foreign key field of Operator is OperatorId (and similar for other) so you need to map it. If you map it with data annotation, for each foreign key field you need to map the field name, i.e.
[ForeignKey("OperatorId")]
public virtual Operator Operator { get; set; }
public Guid OperatorId { get; set; }
I have a table in SqlServerDatabase. Table name is User(Id,Name,Paswd) and Im using automapper in Mvc4. Now i want only specific fields or 2 fields from the table instead of whole table, using automapper.how to do??
basically if the 2 objects have the same fields as in the little example
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Paswd { get; set; }
}
public class UserDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Paswd { get; set; }
}
You just have to ignore the field
Mapper.CreateMap<User, UserDto>()
.ForMember(o => o.Paswd, m => m.Ignore());
You can find a lot of usefull example and features here
Automapepr Wiki
so I'm new to NHibernate and have a problem. Perhaps somebody can help me here.
Given a User-class with many, many properties:
public class User
{
public virtual Int64 Id { get; private set; }
public virtual string Firstname { get; set; }
public virtual string Lastname { get; set; }
public virtual string Username { get; set; }
public virtual string Email { get; set; }
...
public virtual string Comment { get; set; }
public virtual UserInfo LastModifiedBy { get; set; }
}
Here some DDL for the table:
CREATE TABLE USERS
(
"ID" BIGINT NOT NULL ,
"FIRSTNAME" VARCHAR(50) NOT NULL ,
"LASTNAME" VARCHAR(50) NOT NULL ,
"USERNAME" VARCHAR(128) NOT NULL ,
"EMAIL" VARCHAR(128) NOT NULL ,
...
"LASTMODIFIEDBY" BIGINT NOT NULL ,
) IN "USERSPACE1" ;
Database-table-field 'LASTMODIFIEDBY' holds for auditing purposes the Id from the User who is acting in case of inserts or updates. This would normally be an admin.
Because the UI shall display not this Int64 but admins name (pattern like 'Lastname, Firstname') I need to retrieve these values by self referencing table USERS to itself. Next is, that a whole object of type User would be overkill by the amount of unwanted fields. So there is a class UserInfo with much smaller footprint.
public class UserInfo
{
public Int64 Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string FullnameReverse
{
get { return string.Format("{0}, {1}", Lastname ?? string.Empty, Firstname ?? string.Empty); }
}
}
So here starts the problem. Actually I have no clue how to accomplish this task. Im not sure if I also must provide a mapping for class UserInfo and not only for class User. I'd like to integrate class UserInfo as Composite-element within the mapping for User-class. But I dont no how to define the mapping between USERS.ID and USERS.LASTMODIFIEDBY table-fields.
Hopefully I decribes my problem clear enough to get some hints. Thanks alot!
This looks like a bad case or premature optimization.
Are you really having any performance issues if you define LastModifiedBy as User and use <many-to-one> to map it?
That should be your starting point.