My ultimate objective is to reference a table and delete its contents.
there is a code block in my 'OnModelCreating' of dbContext :
modelBuilder.Entity<SpecialOfferVersion>()
.HasMany(x => x.Airports)
.WithMany()
.Map(x =>
{
x.ToTable("SpecialOfferVersionAirport");
x.MapLeftKey("SpecialOfferVersionId");
x.MapRightKey("AirportCode");
});
I want to reference Table 'SpecialOfferVersionAirport',How can I do that?
I tried
modelBuilder.Entity<SpecialOfferVersionAirport>().ToTable("SpecialOfferVersionAirport");
but errors :
Schema specified is not valid. Errors:
(6637,6) : error 0019: The EntitySet 'SpecialOfferVersionAirport1' with schema 'dbo' and table 'SpecialOfferVersionAirport' was already defined. Each EntitySet must refer to a unique schema and table.
Related
I want access data of VPD table outside of its Schema Database. In other words I want to access data of a VPD tables into PDB of another Schema.
Let say database(CDB) name is DEVDB.
Now, Lets consider I have created a schema in DEVDB called : VPDSchema
I created two tables in VPDSchema:
1) DBNAME(DB_Name)
2) DBUSER(DB_Name, Username)
Now I created a Table in VPDSchema called: VPDTable ( data is to filtered on basis of i.e.DBName Column)
So the details are:
Schema Name: VPDSchema
Tables Name: VPDTable
User: VPDSchema_User
Password:******
DataBase:DEVDB
Now I applied below Function and Policy on VPDTable inside VPDSchema
Function: VPDFunction
//Matches from DBUSER &
Return Respective DB
//POLICY
BEGIN
SYS.DBMS_RLS.ADD_POLICY(
object_schema => 'VPDSchema',
object_name => 'VPDTable',
policy_name => 'policy_vpd1',
function_schema => 'vpdSchema',
policy_function => 'vpdFuction',
statement_types => 'select'
);
END;
/
Now let's consider I have created another schema Called: UserSchema
So,
Schema Name : UserSchema
User : UsersSchema_User
password : *******
DataBase : DEVDB
Now,
I created a PDB for a specific user on for this schema (UserSchema)
PDB Created : PDBDB
PDB created for : UserSchema
username : UserSchema_User
password : ****
DataBase : PDBDB
Now the problem is: When I create view of VPDtable inside Userschema for UserSchema_user#DEVDB, I can view data of it as both share same CDBC (DEVDB)
BUT when I create view of VPDtable for inside PDB like userSchema_user#PDBDB, it return 0 records
I tried adding policy in UserSchema but it didn't work.
I am stuck here, please help
I'm new to the WIX web development platform.
I need to use the Primary Key of Table 1 to be used as a reference of Table 2, but I cannot use the ID of table 1 for this purpose.
I wonder if the best way is to "copy" this ID in the Title field (Primary Key) of this table 1. How I do that? Is this the best method?
Thank you,
Arturo
Arturo:
Have you tried doing this without using wix code?
Check out this post to see if it is possible.
Now in code the only way to add a reference field from another data collection is to use the ID. But note that ID is the field name used in the data collection for dashboard and Editor view. When accessing the ID value in code you need to use the field key which is _id.
So in your Table2 you need a column (field) that is of type reference and give it a field name like "Table1 Reference". The editor will generate a field key for you that will look like table1Reference.
Now if you have a record from Table1 that you want to link to Table2 you do something like this:
wixData.query('Table1')
.eq('title', 'uniqueTitle')
.find()
.then((results) => {
if (results.totalCount !== 1) {
throw Error('We didn't get a record from Table1');
}
// Success add this in a new record in Table2
let table1Item = results.items[0];
let table2Data = {
table1Reference:table1Item._id,
anotherTable2Field:"Some info for table2"
};
return wixData.save('Table2', table2Data)
.then((savedRecord) => {
// Successful save!
// Do something here....
});
})
.catch((error) => {
console.log(error);
});
Good luck!
Steve
I create a model that hasnt any relation to another tables with EF.
when I add it in the context and run app for create database i had an Error in Application-start:
One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'UrlHelper' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'RequestContext' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'HttpContextBase' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'Exception' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'Type' has no key defined. Define the key for this EntityType.
Code in my global.asax is like below:
public class MyInitializer
: DropCreateDatabaseIfModelChanges<DBTa>
{
}
protected void Application_Start()
{
//for creating and initializing database
System.Data.Entity.Database.SetInitializer(new MyInitializer());
**DBTa db = new DBTa();**//Error shows here
db.Database.Initialize(true);
//
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
but when i removed model from app my problem solved, but i need this Model.
please help
thanks a lot
You should set "key" property in your 'UrlHelper' model.
If you use EF code first - in your model you should add attribute [Key] to one of its properties.
If you use model first approach - in your model you should mark one of properties as primary key.
If you use Database first approach - you should create primary key in table.
I looked at this SO question.
I want to do something similar in EF 5. I don't see the ForeignKey attribute but instead an Association attribute in EF5.
Also, can someone explain what this does/means :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Match>()
.HasRequired(m => m.HomeTeam)
.WithMany(t => t.HomeMatches)
.HasForeignKey(m => m.HomeTeamId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Match>()
.HasRequired(m => m.GuestTeam)
.WithMany(t => t.AwayMatches)
.HasForeignKey(m => m.GuestTeamId)
.WillCascadeOnDelete(false);
}
This is the explanation :
Primary keys are mapped by default convention. Team must have two
collection of matches. You can't have single collection referenced by
two FKs. Match is mapped without cascading delete because it doesn't
work in these self referencing many-to-many.
What I want to do is very similar to the example in the link but I don't know :
When I need to modify DbContext
When the primary keys will link to each other
When I need to explicitly use Association to create the relationship
Any explanation is appreciated.
Okay so... I can't answer the ForeignKey attribute question on EF beta for I haven't had the chance to check it out yet.
However...
modelBuilder.Entity<Match>() - Take the entity "Match" and perform following operations on it
.HasRequired(m => m.HomeTeam) - The entity needs to have a non-null navigation HomeTeam...
.WithMany(t => t.HomeMatches) - ... which has a subset of Matches by navigation HomeMatches
.HasForeignKey(m => m.HomeTeamId) ... and the associating foreign key is HomeTeamId on Match
.WillCascadeOnDelete(false); ... and don't cascade when the entity is deleted.
That's the beauty of LINQ, it's more often than not self-documenting.
Now, as for your three questions...
Only modify DbContext when you are changing your model relations or adding/deleting an entity. If you're adding, you need to do a
public DbSet Entities { get; set;
And remove it if deleting, etc.
Primary keys don't link to each other. Foreign Keys link to Primary Keys. By convention, if you have a ProjectId, a navigation object called Project and another entity called Project with a property called Id, it will automatically map ProjectId from the first entity to the Id of Project entity and give the Project entity as a navigation item to first entity when you fetch data from the DB via EF:
Only if you need non-convention based relationships. Ie, your primary keys are along the lines of "tblId" or "ParentId" instead of "Id" and "ProjectId", for example. Or you need a different kind of behaviour on some items, such as cascading on deletion for only select entities.
In EF 5, if you're using Migrations, you can alter the migration code to not implement the cascade delete:
CreateTable(
"dbo.Match",
c => new
{
MatchId = c.Long(nullable: false, identity: true),
Description = c.String(),
HomeTeamId = c.Long(nullable: false),
})
.PrimaryKey(t => t.MatchId)
.ForeignKey("dbo.Team", t => t.HomeTeamId, cascadeDelete: false)
.Index(t => t.MatchId)
.Index(t => t.HomeTeamId);
}
Or something like that.
I am integrated with a legacy Oracle database which uses assigned VARCHAR2 values for primary keys. I am creating a one-to-many relationship with this existing table. The legacy table is called Applications (which I may not alter) and the new table is called Projects. Many projects may be assigned to one application.
When GORM creates the Project table it is creating a NUMBER column for the foreign key, application_id, even though this is a VARCHAR2 field in the Applications table.
class Application {
static hasMany = [projects : Project]; // does not fix problem
String application_id;
...
static mapping = {
table 'applications'
version false
id (column:'application_id')
}
static constraints = {
application_id(maxSize:16,blank:false,unique:true,type:"string",generator:"assigned")
}
...
}
class Project {
Application application;
...
}
When I compile the app I get warnings like this:
Unsuccessful: alter table project add constraint FKED904B1956694CB5 foreign key (application_id)
ORA-02267: column type incompatible with referenced column type
When I run the app and click on Application controller I get this error:
SQL state [99999]; error code [17059]; Fail to convert to internal representation; nested exception is java.sql.SQLException: Fail to convert to internal representation
When I click on Project | create I get this error:
Fail to convert to internal representation; nested exception is java.sql.SQLException: Fail to convert to internal representation at /project/create:172
So how can I set the Project class to expect a VARCHAR2 foreign key for the Application?
Thanks for any help!
Look at this site. Maybe it will help you.
Here is the correction in the Application class... in case anyone else searches for this:
class Application {
static hasMany = [projects : Project];
String application_id;
String id // <--- part of solution
static mapping = {
id column:'application_id',generator:'assigned' // <--- part of solution
}
static constraints = {
application_id(maxSize:16,blank:false,unique:true) // <--- part of solution
columns { // <--- part of solution
id type:'text'
application_id type:'text'
}
}